0

我正在尝试遵循以下帖子中介绍的模式:

asp.net MVC 模型如何生成输入名称?

我遇到了一个问题。我已将代码完全按照显示的方式复制到我自己的应用程序中,但是当表单回发时,模型的角色集合的 RoleName 属性为 null。从复选框发布的值是正确的,但 RoleName 不具有约束力。

下面是我正在使用的代码:

注册.cshtml

@model SimpleMembershipTest.Models.RegisterViewModel
@{
ViewBag.Title = "Register";
}

<hgroup class="title">
<h1>@ViewBag.Title.</h1>
<h2>Create a new account.</h2>
</hgroup>

 @using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary()

<fieldset>
    <legend>Registration Form</legend>
    <ol>
        <li>
            @Html.LabelFor(m => m.UserName)
            @Html.TextBoxFor(m => m.UserName)
        </li>
        <li>
            @Html.LabelFor(m => m.FirstName)
            @Html.TextBoxFor(m => m.FirstName)
        </li>
        <li>
            @Html.LabelFor(m => m.LastName)
            @Html.TextBoxFor(m => m.LastName)
        </li>
        <li>
            @Html.LabelFor(m => m.Password)
            @Html.PasswordFor(m => m.Password)
        </li>
        <li>
            @Html.LabelFor(m => m.ConfirmPassword)
            @Html.PasswordFor(m => m.ConfirmPassword)
        </li>
        <li>
            @Html.LabelFor(m => m.Email)
            @Html.TextBoxFor(m => m.Email)
        </li>
    </ol>
    <div class="roleCheckBoxes">
            @Html.EditorFor(m => m.Roles)
        </div>
    <input type="submit" value="Register" />
</fieldset>
}

@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}

RegisterViewModel.cs

namespace SimpleMembershipTest.Models
{
public class RegisterViewModel
{
[Required]
[Display(Name = "User name")]
public string UserName { get; set; }

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

[Required]
[Display(Name = "Last name")]
public string LastName { 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]
[EmailAddress]
[Display(Name = "Email")]
public string Email { get; set; }

public IEnumerable<RoleViewModel> Roles { get; set; }
  }
}

角色视图模型.cs

 @model SimpleMembershipTest.Models.RoleViewModel

@Html.CheckBoxFor(m => m.Selected)
@Html.LabelFor(m => m.Selected, Model.RoleName)

AccountController.cs

[AllowAnonymous]
public ActionResult Register()
{
  var roles = Roles.GetAllRoles();
  var model = new RegisterViewModel
  {
    Roles = roles.Select(role => new RoleViewModel
    {
      RoleName = role,
      Selected = false
    })
  };
  return View(model);
}

我可能遗漏了一些明显的东西,但我似乎看不到它。任何帮助将不胜感激。

4

1 回答 1

3

问题可能是html-inputRoleNameRoleEditor. 例如,您可以使用隐藏输入添加它。

@model SimpleMembershipTest.Models.RoleViewModel

@Html.CheckBoxFor(m => m.Selected)
@Html.LabelFor(m => m.Selected, Model.RoleName)

@Html.HiddenFor(m => m.RoleName)

这篇文章也可能有用http://dotnetslackers.com/articles/aspnet/Understanding-ASP-NET-MVC-Model-Binding.aspx

于 2013-05-19T20:16:52.483 回答