0

次の投稿に示されているパターンに従おうとしています。

asp.net MVCモデルはどのように入力名を生成しますか?

そして私は問題に遭遇しました。表示されているとおりにコードを自分のアプリケーションにコピーしましたが、フォームがポストバックされると、モデルのロール コレクションの RoleName プロパティが null になります。チェックボックスからポストされた値は正しいですが、RoleName は拘束力がありません。

以下は私が使用しているコードです:

Register.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; }
  }
}

RoleViewModel.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-inputforRoleNameがないことRoleEditorです。たとえば、非表示の入力で追加できます。

@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 に答える