MVC3为什么我发布的模型为NULL?

时间:2012-09-01 18:47:56

标签: asp.net-mvc-3

MVC3的好奇问题,EF Code First。

我正在传递一个将两个模型包装到视图中的模型。

 public class UserInfoModel
{
    [Key]
    [Required]
    public int Id { get; set; }

    /// <summary>
    /// <para>Corresponds to ProviderUserKey in ASP Membership</para>
    /// <para>Used in Membership.GetUser(ProviderUserKey) to retrieve email and username.</para>
    /// </summary>
    public Guid MembershipId { get; set; }

    [Required]
    [DataType(DataType.Text)]
    [Display(Name = "First name")]
    public string FirstName { get; set; }

    [Required]
    [DataType(DataType.Text)]
    [Display(Name = "Last name")]
    public string LastName { get; set; }

    [Required]
    [DataType(DataType.Text)]
    [Display(Name = "Address 1")]
    public string Address1 { get; set; }

    [DataType(DataType.Text)]
    [Display(Name = "Address 2")]
    public string Address2 { get; set; }

    [Required]
    [DataType(DataType.Text)]
    [Display(Name = "State")]
    public string State { get; set; }

    [Required]
    [DataType(DataType.Text)]
    [Display(Name = "Country")]
    public string Country { get; set; }

    [Required]
    [DataType(DataType.Text)]
    [Display(Name = "Zip code")]
    public string ZipCode { get; set; }

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

    [Required]
    [DataType(DataType.Date)]
    [Display(Name = "Sign up date")]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = @"{0:MM/dd\/yyyy}")]
    public DateTime SignUpDate { get; set; }

    [Required]
    [DataType(DataType.Date)]
    [Display(Name = "Birthday")]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = @"{0:MM\/dd\/yyyy}")]
    public DateTime BirthDate { get; set; }

    [DataType(DataType.Date)]
    [Display(Name = "Enrollment date")]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = @"{0:MM\/dd\/yyyy}")]
    public DateTime EnrollmentDate { get; set; }

    [DataType(DataType.ImageUrl)]
    [Display(Name = "Avatar")]
    public string AvatarImage { get; set; }

    [Display(Name = "Rank")]
    public int RankId { get; set; }

    [ForeignKey("RankId")]
    public UserRankModel Rank { get; set; }

    [Display(Name = "IsActive")]
    public bool IsActive { get; set; }

}

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

    [Required]
    [DataType(DataType.EmailAddress)]
    [Display(Name = "Email")]
    public string Email { 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; }
}

public class UserInfoAndRegisterModel
{
    public UserInfoModel UserInfoModel { get; set; }
    public RegisterModel RegisterModel { get; set; }
}

查看:

@model K2Calendar.Models.UserInfoAndRegisterModel
@{
ViewBag.Title = "Update User";
}
<div class="container">
    <h2>
        Update Account Details</h2>
    <p>
        Use the form below to update the account.
    </p>
    <p>
        @Html.ValidationSummary(true, "Account update was unsuccessful. Please correct the errors and try again.")
    </p>
    <script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript">    </script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>
@using (Html.BeginForm())
{
    @Html.HiddenFor(m => m.UserInfoModel.Id)
    @Html.HiddenFor(m => m.UserInfoModel.MembershipId)
    @Html.HiddenFor(m => m.UserInfoModel.SignUpDate) 
    <div class="row-fluid">
        <div class="span6">
            @Html.LabelFor(m => m.RegisterModel.UserName)
            @Html.TextBoxFor(m => m.RegisterModel.UserName, new { tabindex = "1" , disabled = "disabled" } )
            @Html.ValidationMessageFor(m => m.RegisterModel.UserName)
        </div>
        <div class="span6">
            @Html.LabelFor(m => m.UserInfoModel.State)
            @Html.TextBoxFor(m => m.UserInfoModel.State, new { tabindex = "8" })
            @Html.ValidationMessageFor(m => m.UserInfoModel.State)
        </div>
    </div>

    <div class="row-fluid">
        <div class="span6">
            @Html.LabelFor(m => m.RegisterModel.Email)
            @Html.TextBoxFor(m => m.RegisterModel.Email, new { tabindex = "2" , disabled = "disabled" })
            @Html.ValidationMessageFor(m => m.RegisterModel.Email)
        </div>
        <div class="span6">
            @Html.LabelFor(m => m.UserInfoModel.ZipCode)
            @Html.TextBoxFor(m => m.UserInfoModel.ZipCode, new { tabindex = "9" })
            @Html.ValidationMessageFor(m => m.UserInfoModel.ZipCode)
        </div>
    </div>

    <div class="row-fluid">
        <div class="span6"> 
            @Html.LabelFor(m => m.UserInfoModel.FirstName)
            @Html.TextBoxFor(m => m.UserInfoModel.FirstName, new { tabindex = "3" })
            @Html.ValidationMessageFor(m => m.UserInfoModel.FirstName)
        </div>
        <div class="span6">
            @Html.LabelFor(m => m.UserInfoModel.Country)
            @Html.TextBoxFor(m => m.UserInfoModel.Country, new { tabindex = "10" })
            @Html.ValidationMessageFor(m => m.UserInfoModel.Country)
        </div>
    </div>

    <div class="row-fluid">
        <div class="span6">
            @Html.LabelFor(m => m.UserInfoModel.LastName)
            @Html.TextBoxFor(m => m.UserInfoModel.LastName, new { tabindex = "4" })
            @Html.ValidationMessageFor(m => m.UserInfoModel.LastName)
        </div>
        <div class="span6">
            @Html.LabelFor(m => m.UserInfoModel.BirthDate)
            @Html.TextBoxFor(m => m.UserInfoModel.BirthDate, new { tabindex = "11", placeholder = "mm/dd/yyyy" })
            @Html.ValidationMessageFor(m => m.UserInfoModel.BirthDate)
        </div>
    </div>

    <div class="row-fluid">
        <div class="span6">
            @Html.LabelFor(m => m.UserInfoModel.Address1)
            @Html.TextBoxFor(m => m.UserInfoModel.Address1, new { tabindex = "5" })
            @Html.ValidationMessageFor(m => m.UserInfoModel.Address1)
        </div>
        <div class="span6">
            @Html.LabelFor(m => m.UserInfoModel.EnrollmentDate)
            @Html.TextBoxFor(m => m.UserInfoModel.EnrollmentDate, new { tabindex = "12", placeholder = "mm/dd/yyyy" })
            @Html.ValidationMessageFor(m => m.UserInfoModel.EnrollmentDate)
        </div>
    </div>

    <div class="row-fluid">
        <div class="span6">
            @Html.LabelFor(m => m.UserInfoModel.Address2)
            @Html.TextBoxFor(m => m.UserInfoModel.Address2, new { tabindex = "6" })
            @Html.ValidationMessageFor(m => m.UserInfoModel.Address2)
        </div>
        <div class="span6">
            @Html.LabelFor(m => m.UserInfoModel.RankId)
            @Html.DropDownListFor(m => m.UserInfoModel.RankId, (SelectList)ViewBag.RankList, new { tabindex = "13" })
        </div>
    </div>

    <div class="row-fluid">
        <div class="span6">
            @Html.LabelFor(m => m.UserInfoModel.PhoneNumber)
            @Html.TextBoxFor(m => m.UserInfoModel.PhoneNumber, new { tabindex = "7" })
            @Html.ValidationMessageFor(m => m.UserInfoModel.PhoneNumber)
        </div>
        <div class="span6">

        </div>
    </div>

    <br />
    <p>
        <button type="submit" class="btn btn-primary btn-large" tabindex = "14">Update &raquo;</button>
    </p>
}

我的控制器类采用发布的模型并执行一些数据库更新。

其中一个包装模型为null:

[Authorize]
    [HttpPost]
    public ActionResult Edit(UserInfoAndRegisterModel model)
    {
        //WHY IS model.RegisterModel == null ??//
        if (ModelState.IsValid)
        {
            try
            {
                dbContext.Entry(model.UserInfoModel).State = System.Data.EntityState.Modified;
                dbContext.SaveChanges();
                return RedirectToAction("Index", "Home");
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException("Failed to update UserInfoModel", ex.InnerException);
            }
        }
        GenerateRanksList();
        return View(model);
    }

我为帐户创建做了同样的事情,并且我没有收到RegisterModel的空值,只有几乎相同的视图代码。

目前这是一个非问题,因为我目前只更新UserInfoModel,但将来我可能希望允许用户更改其电子邮件地址或用户名。

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

您的RegisterModel为空,因为您没有发布任何属于RegisterModel

的值

虽然您在视图中有类似RegisterModel的输入

@Html.TextBoxFor(m => m.RegisterModel.UserName, 
                 new { tabindex = "1" , disabled = "disabled" } )

它们都是disabled,并且未发布已禁用的输入:

Form submission - Successful controls

  

成功控制对提交“有效”

     

然而:

     

禁用的控件无法成功。

因此,您需要删除disable或将属性添加为隐藏字段:

@Html.HiddenFor(m => m.RegisterModel.UserName)