数据绑定:“ System.String”不包含名称为“ Name”的属性

时间:2019-05-26 09:23:02

标签: c# asp.net-mvc entity-framework security asp.net-identity

我正在使用asp.net mvc进行自定义身份验证,我正在使用角色提供程序。我与用户,角色,UserInRoles有很多关系。 视图中复选框中绑定角色值的错误

我尝试调试控制器的create方法,该方法将返回Role表中的所有数据,然后继续在foreach中查看

  

错误数据绑定:'System.String'不包含名称为'Name'的属性。

用户模型

public class User
    {
        [Key]
        public string  UserId { get; set; }
        public string Name { get; set; }
        public virtual ICollection<UserInRoles> UserInRoles { get; set; }

    }

角色模型

public class Role
    {
        public int RoleId { get; set; }
        public string Name { get; set; }
        public virtual ICollection<UserInRoles> UserInRoles { get; set; }


    }

UserInRole模型

 public class UserInRoles
    {
        [Key, Column(Order = 0)]
        public string UserId { get; set; }
        [Key, Column(Order = 1)]
        public int RoleId { get; set; }
        public virtual User User { get; set; }
        public virtual Role Role { get; set; }

    }

控制器

public ActionResult Create()
        {
            ViewBag.RoleId = new SelectList(Roles.GetAllRoles().ToList(), "Name", "Name");
            return View();
        }

查看

@model AramexOneKnowledge.Models.RegisterViewModel
@{
    ViewBag.Title = "Create";
}
<h2>Create</h2>

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

<div class="form-horizontal">
    <h4>User</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="form-group">
        @Html.LabelFor(model => model.UserId, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.UserId, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.UserId, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
        </div>
    </div>
    <div class="form-group">
        <label class="col-md-2 control-label">
            Select User Role
        </label>
        <div class="col-md-10">
            @foreach (var item in (SelectList)ViewBag.RoleId)
            {
                <input type="checkbox" name="SelectedRoles"
                       value="@item.Value" class="checkbox-inline" />
                @Html.Label(item.Value, new { @class = "control-label" })
            }
        </div>
    </div>
    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Create" class="btn btn-primary" />
        </div>
    </div>

复选框中的预期结果显示角色

1 个答案:

答案 0 :(得分:1)

此异常是因为Roles.GetAllRoles()返回了string[],而您尝试将其中不存在的属性Name绑定到其中。

因此,要解决此问题,您可以按照以下步骤填充复选框列表

ViewBag.RoleId = Roles.GetAllRoles().Select(r => new SelectListItem{Text = r, Value = r});

在这种情况下,您SelectListItem ValueText将是他们两个的角色名称