0

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)
    {

    }
0

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I had the same problem with misspelt options aswell
1

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.

1 Comment

MVC 3 includes built in support for JSON model binding

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.