DataBinding: 'System.String' 不包含名称为 'Name' 的 属性

DataBinding: 'System.String' does not contain a property with the name 'Name'

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

我尝试调试控制器创建方法return角色中的所有数据table然后继续在foreach中查看

error DataBinding: 'System.String' does not contain a property with the name '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>

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

此异常是因为 Roles.GetAllRoles() returns string[] 并且您尝试将 属性 Name 绑定到它不存在的内部。

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

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

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