asp.net-mvcasp.net-mvc-4asp.net-ajaxasp.net-mvc-ajax

How to receive model object sent by ajax call in a controller in asp.net mvc


I sent an object to controllers action thorugh ajax , but I don't know how to recevie the object in controller. My ajax call is :

       $.ajax({
            type: "POST",
            url: '@Url.Action("Create","Home")',
            data: { ID: '@Model.ID', Name: '@Model.Name'}
        }).done(function (msg) {
            alert("Added :" + msg);
        });

This should work , BUt I can't figure out how to recevie the object in controller. I wrote this :

    public ActionResult Create(Home h)
    {

    }

But it is not working . I neeed help in this , Thanks in advance.

My Home class :

public class Home
{
    public int ID { get; set; }
    public string Name { get; set; }
}

Solution

  • Your action should be like thus:

    [HttpPost]
    public ActionResult Create(Home h)
    {
        throw new NotImplementedException(); // put you code here
    }