Search code examples
asp.netasp.net-mvcasp.net-ajaxasp.net-mvc-ajax

How to retrieve list of objects from controller using ajax in asp.net mvc


I want to retrieve list of objects from controller and pass it to the view using ajax. My controller's Get action's code is :

       public ActionResult Get()
        {
            Home h = new Home();
            return View(h.get());
        }

h.get() returns a list , which I want to send back to the ajax call written in the view. Ajax call :

<script type="text/javascript">
        $.ajax({
            type: "GET",
            url: '@Url.Action("Get","Home")',
        }).done(function (msg) {
            alert(msg);
        });
</script>

How can I transfer data from controller to view ? I need help in this , Thanks in advance


Solution

  • You probably should return the data as JSON.

    public ActionResult Get()
            {
                Home h = new Home();
                return Json(h.get(), JsonRequestBehavior.AllowGet);
            }