2012-09-01 53 views
1

MVC3的好奇問題,EF Code First。MVC3爲什麼我發佈的模型爲NULL?

我傳遞了一個模型,將兩個模型包裝到視圖中。

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

我的控制器類需要被張貼的模型,並做一些數據庫的更新。

其中一個包裹機型爲空:

[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,但將來我可能想要允許用戶更改其電子郵件地址或用戶名。

任何想法?

回答

2

RegisterModel爲null,因爲你雖然你有屬於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) 
+0

你是100%正確。刪除'禁用'使模型非空。發佈代碼後,我認爲我會得到一個填充null屬性的非空模型。 –

相關問題