How do I pass this js array to my MVC 3 controller?

前端 未结 7 2153
粉色の甜心
粉色の甜心 2020-12-31 16:58

I am getting null values in the controller. Not sure what am I am missing.

I have a grid where I have a list of guests (with name & email) where user select gues

7条回答
  •  南笙
    南笙 (楼主)
    2020-12-31 17:30

    You get null because you're sending the json array in wrong way.

    First, you should serialize your json array:

    $.ajax({
            // Whatever ...
            type: "POST",
            url: "yourUrl",
            data: JSON.stringify(guests),
            // Whatever ...
        });
    

    Second, your controller should get a string:

    [HttpPost]
    public ActionResult ActionName(string guests)
    {
        // ...
    }
    

    and finally, you should deserialize that string to the related type:

    [HttpPost]
    public ActionResult ActionName(string guests)
    {
        // this line eserializes guests ...
    
        IList gs = 
           new JavaScriptSerializer().Deserialize>(guests);
    
        // ... Do whatever with gs ...
    
        return View();
    }
    

提交回复
热议问题