开发者

asp.net mvc 3 json values not recieving at controller

the problem is that i am not able to recieve any value in the controller . what could be wrong? the code is here.

  $('#save').click(function () {

        var UserLoginViewModel = { UserName: $('vcr_UserName').val(),
            Password: $('vcr_Password').val()
        };
 开发者_如何转开发       $.ajax({
            url: "/User/Login",
            data: JSON.stringify(UserLoginViewModel),
            contenttype: "application/json; charset=utf-8",
            success: function (mydata) {
                $("#message").html("Login");
            },
            error: function () {
                $("#message").html("error");
            },
            type: "POST",
            datatype: "json"
        });
        return false;
    });
});

    [HttpPost]
    public ActionResult Login(UserLoginViewModel UserLoginViewModel)
    {

    }


As you're using MVC3 - you should be able to take advantage of the built in JSON model binding.

Your code example has a couple of typos: contentType and dataType are lowercase...(they should have an uppercase "T")

jQuery ajax docs

After you POST up the correct contentType/dataType, MVC should automatically bind your object to the posted JSON.


You're going to need an action filter or similar to intercept the json from the post body.

Here's a starter

Provider Factory

but here is the article that sorted this for me On Haacked

It is good if you know the type you are deserialising into up front, but if you need polymorphism you'll end up using these ideas in an action filter.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜