[英]DataBinding: 'System.String' does not contain a property with the name 'Name'
i'm working with custom authentication with asp.net mvc , i'm using Role Provider. 我正在使用asp.net mvc进行自定义身份验证,正在使用角色提供程序。 i have many to many relation User , Role , UserInRoles . 我与用户,角色,UserInRoles有很多关系。 the error in binding roles values in checkboxs in view 视图中复选框中绑定角色值的错误
i tried debugging controller create method it return all data in Role table then continue to view in foreach 我尝试调试控制器的create方法,它返回Role表中的所有数据,然后继续在foreach中查看
error DataBinding: 'System.String' does not contain a property with the name 'Name'. 错误数据绑定:'System.String'不包含名称为'Name'的属性。
User Model 用户模型
public class User
{
[Key]
public string UserId { get; set; }
public string Name { get; set; }
public virtual ICollection<UserInRoles> UserInRoles { get; set; }
}
Role Model 榜样
public class Role
{
public int RoleId { get; set; }
public string Name { get; set; }
public virtual ICollection<UserInRoles> UserInRoles { get; set; }
}
UserInRole Model 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; }
}
Controller 调节器
public ActionResult Create()
{
ViewBag.RoleId = new SelectList(Roles.GetAllRoles().ToList(), "Name", "Name");
return View();
}
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>
expected result display roles in checkbox 预期结果在复选框中显示角色
this exception because Roles.GetAllRoles()
returns string[]
and you try to bind property Name
inside it which it not exists. 发生此异常是因为Roles.GetAllRoles()
返回string[]
并且您尝试在其中绑定属性Name
该属性不存在)。
so to overcome this issue you can populate you checkbox list as following 因此,要解决此问题,您可以如下填充复选框
ViewBag.RoleId = Roles.GetAllRoles().Select(r => new SelectListItem{Text = r, Value = r});
in this case you SelectListItem
Value
and Text
it will be the role name for both of them 在这种情况下,您的SelectListItem
Value
和Text
将是这两个角色的名称
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.