【问题标题】:asp.net mvc model properties are nullasp.net mvc 模型属性为空
【发布时间】:2014-05-13 13:00:08
【问题描述】:

ModelState.IsValid = false。为什么 RoleName 属性为空?

UserRoles.cshtml:

@model MtmOspWebApplication.Models.SelectUserRolesViewModel

@{
    ViewBag.Title = "UserRoles";
}

<h2>Roles for User @Html.DisplayFor(model => model.UserName)</h2>
<hr />

@using (Html.BeginForm("UserRoles", "Account", FormMethod.Post, new { encType = "multipart/form-data", name = "myform" }))
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        @Html.ValidationSummary(true)
        <div class="form-group">
            <div class="col-md-10">
                @Html.HiddenFor(model => model.UserName)
            </div>
        </div>

        <h4>Select Role Assignments</h4>
        <br />
        <hr />

        <table cellspacing="10" cellpadding="5">
            <tr>
                <th>
                    Role Name
                </th>
                <th>
                    Selected
                </th>
            </tr>
            @Html.EditorFor(model => model.Roles)
        </table>
        <br />
        <hr />

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Save" class="btn btn-default" />
            </div>
        </div>
    </div>
}

SelectRoleEditorViewModel.cshtml:

@model MtmOspWebApplication.Models.SelectRoleEditorViewModel

<tr>
    <td>@Html.DisplayFor(model => model.RoleName)</td>
    <td>@Html.CheckBoxFor(model => model.Selected)</td>
</tr>

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

Model Classes:

public class SelectUserRolesViewModel
    {
        public SelectUserRolesViewModel()
        {
            this.Roles = new List<SelectRoleEditorViewModel>();
        }


        // Enable initialization with an instance of ApplicationUser:
        public SelectUserRolesViewModel(ApplicationUser user)
            : this()
        {
            this.UserName = user.UserName;
            this.FirstName = user.FirstName;
            this.LastName = user.LastName;

            var Db = new ApplicationDbContext();

            // Add all available roles to the list of EditorViewModels:
            var allRoles = Db.Roles;
            foreach (var role in allRoles)
            {
                // An EditorViewModel will be used by Editor Template:
                var rvm = new SelectRoleEditorViewModel(role);
                this.Roles.Add(rvm);
            }

            // Set the Selected property to true for those roles for 
            // which the current user is a member:
            foreach (var userRole in user.Roles)
            {
                var checkUserRole =
                    this.Roles.Find(r => r.RoleName == userRole.Role.Name);
                checkUserRole.Selected = true;
            }
        }

        public string UserName { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public List<SelectRoleEditorViewModel> Roles { get; set; }
    }


    // Used to display a single role with a checkbox, within a list structure:
    public class SelectRoleEditorViewModel
    {
        public SelectRoleEditorViewModel() { }
        public SelectRoleEditorViewModel(IdentityRole role)
        {
            this.RoleName = role.Name;
        }

        public bool Selected { get; set; }

        [Required]
        public string RoleName { get; set; }
    }

【问题讨论】:

  • 您在哪里使用 SelectRoleEditorViewModel.cshtml?是局部视图吗?
  • 它在 EditorTemplates 中。
  • 你为什么不简单地使用@Model.RoleName?
  • 因为 EditorFor 会自动处理列表。
  • 你试过LabelFor吗?

标签: c# asp.net razor asp.net-mvc-5


【解决方案1】:

试试

@Html.EditorFor(model => model.RoleName)

而不是

@Html.DisplayFor(model => model.RoleName)

或对 model.RoleName 使用隐藏输入

【讨论】:

  • 但我不希望用户能够编辑 RoleName?使用 hidden 是唯一的方法吗?
  • 您需要输入才能回发到控制器。输入type=hiddentype=textreadonly=true,因此无法编辑
  • 那么我的问题是发布 html 表单。不是模型绑定问题?
  • 或者您可以将复选框的值设置为角色名称,并返回所选角色名称的列表
猜你喜欢
  • 1970-01-01
  • 2021-09-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-13
  • 1970-01-01
  • 2018-11-30
  • 1970-01-01
相关资源
最近更新 更多