Reputation: 2216
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
Upvotes: 0
Views: 1505
Reputation: 194
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);
});
Upvotes: 0
Reputation: 17680
You probably should return the data as JSON.
public ActionResult Get()
{
Home h = new Home();
return Json(h.get(), JsonRequestBehavior.AllowGet);
}
Upvotes: 2