Link to home
Start Free TrialLog in
Avatar of nicedone
nicedone

asked on

i have an MVC application just making a simple search with one controller and view, everything works fine but i cant show my returned values in my view

below is my Index.cshtml , HomeController and script file respectively, i dont seem to get an error and ajax call returns with a response but i somehow fail to show it on the index page , and i cant seem to find the reason why for a while now,
i need you help asap ,thanks

@model TrainTicketMachine.Models.StationViewResponse

@{
    ViewBag.Title = "Index";
}


<form method="GET" class="search" >

    <input type="text" name="searchTerm" id="searchTerm"/>
   


    </form>
<p>

   
<div>
    <table class="center">
     @if (Model.StationList != null)
        {
        <tr>
            <td>
                <h3>Station(s)</h3>


            </td>

        </tr>
        foreach (var item in Model.StationList)
        {
            <tr>
                <td>

                    @Html.DisplayFor(modelItem => item.StationName)

                </td>
            </tr>
        }
    
     }
    </table>

    <table class="center">

        
        @if (Model.RecomLetters != null)
        {
            <tr>
                <td>
                    <h3>Next Possible Letter(s)</h3>


                </td>

            </tr>
            foreach (var item in Model.RecomLetters)
            {
                    <tr>
                        <td>

                          
                            <label for="letter">@item.ToString()</label>

                        </td>
                    </tr>
                }
        }
    </table>
</div>

Open in new window


 public class HomeController : Controller
    {
        [HttpGet]
        public ActionResult Index()
        {
           StationDataModelContainer _db = new StationDataModelContainer();
           //List<Station> result = _db.Stations.Where(s => s.StationName.StartsWith(searchTerm)).ToList();
           StationViewResponse response = new StationViewResponse();


           //if (result != null && result.Count > 0 && !string.IsNullOrEmpty(searchTerm))
           //{
           //    Letters recomendedLetters = new Letters(searchTerm.RecommendLetters(result));
           //    response.RecomLetters = recomendedLetters;
           //    response.StationList = result;
           //    return View(response);

           //}

            return View(response);
        }

        [HttpPost]
        public StationViewResponse Index(string searchTerm = null)
        {
            StationDataModelContainer _db = new StationDataModelContainer();
            List<Station> result = _db.Stations.Where(s => s.StationName.StartsWith(searchTerm)).ToList();
            StationViewResponse response = new StationViewResponse();


            if (result != null && result.Count > 0 && !string.IsNullOrEmpty(searchTerm))
            {
                Letters recomendedLetters = new Letters(searchTerm.RecommendLetters(result));
                response.RecomLetters = recomendedLetters;
                response.StationList = result;
                return response;

            }

            return response;
        }

    }

Open in new window



 <script type="text/javascript">

       $("#searchTerm").on('input', function() {

            console.log('before call');
            var searchTerm = $("#searchTerm").val();
            console.log(searchTerm);
            $.ajax({
                url: '/Home/Index',
                type: 'POST',
                data: JSON.stringify({ 'searchTerm': searchTerm }),  //JSON.stringify({ 'Options': searchTerm }),
                contentType: "application/json",
                dataType: 'json',
                traditional: true,
                success: function (data) {
                    console.log(data); 
                },
                error: function (request, status, error) {
                    alert(error);
                }

           
            })
            console.log('after call');
        });

    </script>

Open in new window

Avatar of nicedone
nicedone

ASKER

In my homecontroller when i changed return to

 
 return Json(response, JsonRequestBehavior.AllowGet);

Open in new window


then as a result of the call i do get the json response which i need to show on my page but i do not know how to do that in jquery, or should i change the type to html  then when i get html should i assign this to whole page's html to see the result i want to see ??
ASKER CERTIFIED SOLUTION
Avatar of Miguel Oz
Miguel Oz
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial