0

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

ShufflerShark
  • 355
  • 1
  • 4
  • 20
Hammad Shahid
  • 1,996
  • 5
  • 26
  • 57

2 Answers2

2

You probably should return the data as JSON.

public ActionResult Get()
        {
            Home h = new Home();
            return Json(h.get(), JsonRequestBehavior.AllowGet);
        }
scartag
  • 16,927
  • 3
  • 44
  • 50
0

You are sending a view back in this case. You should return a JSON.

return Json(result, "text/html", System.Text.Encoding.UTF8, JsonRequestBehavior.AllowGet);

Or if you want to return a view with a model attached to it you could do

return PartialView("Name", model);

and on the view side you load it to the div

$.ajax({
            type: "GET",
            url: '@Url.Action("Get","Home")',
        }).done(function (msg) {
            $("#div-id").html(msg);
        });
user1540857
  • 186
  • 10