MR.ABC
MR.ABC

Reputation: 4862

MVC4 Ajax Post Action Parameter Null

I've a controller with post action. Using javascript ajax to post the data, the action parameter is always null. Any suggestion ?

Routing

    config.Routes.MapHttpRoute(
        null,
        routeTemplate: "api/{controller}/{action}"
    );

Ajax

public register(data: any): bool {
    var url = this.baseUrl + "Account/Register/";
    var xhr: JQueryXHR = $.ajax({
        type: "POST",
        url: url,
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        data: { id: 0, "Username": "myUser", "Password" : "myPass" },
        async: false,
        cache: false,
    });
    return (xhr.status == 200);
}

Action

[HttpPost]
[AllowAnonymous]
[Mvc.ValidateAntiForgeryToken]
public void Register(UserLogin login)

UserLogin

public class UserLogin
{
    public string Username { get; set; }
    public string Password { get; set; }
}

Upvotes: 1

Views: 1412

Answers (1)

Khanh TO
Khanh TO

Reputation: 48982

Try this:

 data: JSON.stringify({ login : { id: 0, "Username": "myUser", "Password" : "myPass" }}),

Upvotes: 2

Related Questions