表单发布

时间:2015-08-14 03:34:27

标签: c# asp.net angularjs asp.net-web-api

您好我使用Angulajs作为前端,ASP.NET WebAPI作为后端。

这是我的Model类:

 public class UserModel
 {
    [Required]
    [Display(Name = "User name")]
    public string UserName { get; set; }

    [Required]
    [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string Password { get; set; }

    [DataType(DataType.Password)]
    [Display(Name = "Confirm password")]
    [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
    public string ConfirmPassword { get; set; }

    [Required]
    [StringLength(50, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 7)]
    [Display(Name = "Email")]
    public string Email { get; set; }

    [Required]
    [Display(Name = "Phone")]
    public string PhoneNumber { get; set; }


 }

控制器:

// POST api/Account/Register
        [AllowAnonymous]
        [Route("Register")]
        public async Task<IHttpActionResult> Register(UserModel userModel)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            IdentityResult result = await _repo.RegisterUser(userModel);

            IHttpActionResult errorResult = GetErrorResult(result);

            if (errorResult != null)
            {
                return errorResult;
            }
            return Ok();
        }

这是我的角色服务:

var _saveRegistration = function (registration) {

return $http.post('http://localhost:26264/api/account/register', registration).then(function (response) {

       return response;
 });
};

我正在发送有效载荷 P.S这是有效载荷

registration:{
    userName:"UserName",
    email: "eamil@emal.com",
    password:"password",
    confirmPassword:"password",
    phone: "9898989898"
  }

当我调试时,我看到有效负载中的所有值,但是当我在API控制器“register”方法字段“PhoneNumber”中设置调试器时为空。 enter image description here 我不知道我在做什么,请帮忙

1 个答案:

答案 0 :(得分:1)

phone更改为phoneNumber

registration:{
    userName:"UserName",
    email: "eamil@emal.com",
    password:"password",
    confirmPassword:"password",
    phoneNumber: "9898989898"
  }