MVC4 Ajax Post Action参数Null

时间:2013-07-06 04:37:28

标签: c# ajax asp.net-mvc asp.net-mvc-4

我有一个带有后期操作的控制器。使用javascript ajax发布数据,action参数始终为null。有什么建议吗?

路由

    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);
}

动作

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

用户登陆

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

1 个答案:

答案 0 :(得分:2)

试试这个:

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