【问题标题】:MVC3 Why is my posted model NULL?MVC3 为什么我发布的模型为 NULL?
【发布时间】:2012-09-01 18:47:55
【问题描述】:

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

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

其中一个包装模型为空:

[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,但将来我可能希望允许用户更改他们的电子邮件地址或用户名。

有什么想法吗?

【问题讨论】:

    标签: asp.net-mvc-3


    【解决方案1】:

    您的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)
    

    【讨论】:

    • 你是 100% 正确的。删除“残疾人”使模型非空。发布代码后,我假设我会得到一个填充了 null 属性的非 null 模型。
    猜你喜欢
    • 1970-01-01
    • 2012-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-18
    • 1970-01-01
    • 1970-01-01
    • 2012-04-14
    相关资源
    最近更新 更多