0

我正在开发一个 asp.net mvc 3.0 并在如下视图中使用复杂模型:

@model StoresAndMalls.DataModel.Entities.User
........
<div class="editor-label">
    @Html.LabelFor(model => model.EmailAddress)
</div>
<div class="editor-field">
    @Html.EditorFor(model => model.EmailAddress)
    @Html.ValidationMessageFor(model => model.EmailAddress)
</div>
<div class="editor-label">
    @Html.LabelFor(model => model.Status)
</div>
<div class="editor-field">
    @Html.EditorFor(model => model.Status)
    @Html.ValidationMessageFor(model => model.Status)
</div>

 <div class="editor-label">
    @Html.LabelFor(model => model.Role)
</div>
<div class="editor-field">
    @Html.EditorFor(model => model.Role)
    @Html.ValidationMessageFor(model => model.Role)
</div>

<div class="editor-label">
    @Html.LabelFor(model => model.UserRules)
</div>
<div class="editor-field">
    @Html.EditorFor(model => model.UserRules,"UserRules", new { AllRules = ViewBag.AllRules})

</div>

这是我的模型:

public partial class User
    {
        ......

        public virtual ICollection<UserRule> UserRules { get; set; }

        public virtual Role Role { get; set; }
    }

 public class UserRule
    {
        .....
        [ForeignKey("UserId")]
        public virtual User User { get; set; }

        [ForeignKey("RuleId")]
        public virtual Rule Rule { get; set; }
    }

public partial class Role
    {
        public int Id { get; set; }

        public string Name { get; set; }

        public virtual ICollection<User> Users { get; set; }
    }

在最近的项目中,当我创建一个具有复杂模型的视图时,表单元素,例如 Role,其中以 User 为前缀,我的意思是像“User.Role”,但现在没有了,即使我使用编辑器模板对于 User 类的 'UserRules' 属性,它也不能正常工作,并且没有 'User' 前缀,

这是我的角色和用户规则的编辑器视图:

@model IEnumerable<StoresAndMalls.DataModel.Entities.UserRule>
@{
    var allrules = (ViewData["AllRules"] as List<StoresAndMalls.DataModel.Entities.Rule>);
    int c = 0;
}
@
    foreach (var item in allrules)
    {

        @item.Description
        @Html.CheckBox(Model.Any(x => x.RuleId == item.Id))
        c++;
    <br />
    }


-----------


@model StoresAndMalls.DataModel.Entities.Role
@{
    var selecetList = (ViewData["Roles"] as List<StoresAndMalls.DataModel.Entities.Role>).
        ConvertAll(x => new SelectListItem { Value = x.Id.ToString(), Text = x.Name, Selected = x.Id == Model.Id });
}
@Html.DropDownListFor(x => x, selecetList)


-------------

对于'UserRule'我试过

@model StoresAndMalls.DataModel.Entities.UserRule

但它抛出:

The model item passed into the dictionary is of type 'System.Collections.Generic.HashSet`1[DataModel.Entities.UserRule]', but this dictionary requires a model item of type 'DataModel.Entities.UserRule'.

编辑:

 public ActionResult UpdateManager(Guid id)
        {
            ViewBag.Roles = unitofwork.RoleRepository.Get();

            ViewBag.AllRules = unitofwork.UserRepository.GetByID(id).UserRules;

            var model = unitofwork.UserRepository.GetByID(id);

            return View(model);
        }
4

0 回答 0