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

2 Answers 2

2

You probably should return the data as JSON.

public ActionResult Get()
        {
            Home h = new Home();
            return Json(h.get(), JsonRequestBehavior.AllowGet);
        }
Sign up to request clarification or add additional context in comments.

2 Comments

I am getting this error "The name 'JSON' doensot exist in the current context" . I have added System.Web.Helpers , but it does not work
@HammadShahid change the case .. it is Json .. not JSON
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);
        });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.