开发者

Passing JSON into MVC Controller - returns always null

we are using MVC 2 on our project.

am getting null value for the object which is sent to controller using jquery/json

please correct me where i made the mistake.

here is my code

on posting da开发者_JS百科ta to controller i wrote below JQuery

$(document).ready(function () {

        $("#frmContact").submit(function () {
                    x = '{"Name":"John","EmailAddress":"john@gmail.com"}';
                    $.ajax({
                        type: 'POST',
                        url: '/dashboard/gmail',
                        data: x,
                        contentType: "application/json; charset=utf-8",
                        dataType: "json"
                        });
             });
});

Note: i tried JSON.stringify(x) also for the data.

on the controller part i wrote:

public class ContactDetail
{
    public string Name { get; set; }
    public string EmailAddress { get; set; }
}

[AcceptVerbs(HttpVerbs.Post)]
[ObjectFilter(Param = "contactDetail", RootType = typeof(ContactDetail))]
public ActionResult gmail(ContactDetail contactDetail)
{
    // for now i didn't wrote any code here
    return View();
}

contactDetail is null.

Thanks in advance


MVC 2 didn't come with a json value provider as standard. Take a look at this Phil Haack blog to explain how to sort it out.


Did you try :

x = {"Name":"John","EmailAddress":"john@gmail.com"};

?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜