[asp.net-mvc]无法填充从 mvc 4 中的数据库数据的复选框

发布时间: 2017/3/25 4:20:38
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

这是我的控制器代码。

public ActionResult Create()
{
    ViewBag.grp_id = new SelectList(db.tm_grp_group, "grp_id", "grp_name");
    ViewBag.perm_id = new SelectList(db.tm_perm_level, "perm_id", "perm_levelname");
    return View();
}

下面是我查看代码。

@model Permission.ts_grp_perm_mapping
....
@using (Html.BeginForm()) {
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>ts_grp_perm_mapping</legend>
        <div class="editor-label">
            @Html.LabelFor(model => model.grp_permid)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.grp_permid)
            @Html.ValidationMessageFor(model => model.grp_permid)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.grp_id, "tm_grp_group")
        </div>
        <div class="editor-field">
            @Html.DropDownList("grp_id", String.Empty)
            @Html.ValidationMessageFor(model => model.grp_id)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.perm_id, "tm_perm_level")
        </div>
        <div class="editor-field">
            @Html.DropDownList("perm_id", String.Empty)
            @Html.ValidationMessageFor(model => model.perm_id)
        </div>
        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

在控制器 ViewBag.perm_id 包含一些值 (这是外键)。在视图中 perm.id dropdownbox 窗体中显示但我想它的形式如何。我如何可以做到这一点?

这是我创建的视图。

public class AssignUserViewModel
{
    public tm_perm_level[] perms { get; set; }
    public int grp_id { get; set; }
}

现在在控制器中如何发送值以查看?这是我 tm_perm_level 模型

public partial class tm_perm_level
{
    public tm_perm_level()
    {
        this.ts_grp_perm_mapping = new HashSet<ts_grp_perm_mapping>();
    }
    public int perm_id { get; set; }
    public string perm_levelname { get; set; }
    public string perm_description { get; set; }
    public bool perm_status { get; set; }
    public virtual ICollection<ts_grp_perm_mapping> ts_grp_perm_mapping { get; set; }
}

这是 ts_grp_perm_mapping 模型

public partial class ts_grp_perm_mapping
{
    public ts_grp_perm_mapping()
    {
        this.ts_perm_levelmapping = new HashSet<ts_perm_levelmapping>();
    }
    public int grp_permid { get; set; }
    public int grp_id { get; set; }
    public int perm_id { get; set; }
    public List<tm_perm_level> permissions { get; set; }

    public virtual tm_grp_group tm_grp_group { get; set; }
    public virtual tm_perm_level tm_perm_level { get; set; }
    public virtual ICollection<ts_perm_levelmapping> ts_perm_levelmapping { get; set; }
}

解决方法 1:

尤其是在编辑时,总是启动与视图模型来表示您想要显示 (请参阅在 MVC 中的视图模型是什么?)

public class PermissionVM
{
  public int ID { get; set; }
  public string Name { get; set; }
  public bool IsSelected { get; set; }
}
public class GroupPermissionVM
{
  public int GroupID { get; set; }
  public IEnumerable<SelectListItem> GroupList { get; set; }
  public IEnumerable<PermissionVM> Permissions { get; set; }
}

然后创建 EditorTemplatePermissionVM 。在 /Views/Shared/EditorTemplates/PermissionVM.cshtml 文件夹

@model PermissionVM
<div>
    @Html.HiddenFor(m => m.ID)
    @Html.HiddenFor(m => m.Name)
    @Html.CheckBoxFor(m => m.IsSelected)
    @Html.LabelFor(m => m.IsSelected, Model.Name)
</div>

主视图将

@model GroupPermissionVM
....
@using (Html.BeginForm())
{
    // dropdownlist
    @Html.LabelFor(m => m.GroupID)
    @Html.DropDownListFor(m => m.GroupID, Model.GroupList, "Please select")
    @Html.ValidationMessageFor(m => m.GroupID)
    // checkboxlist
    @Html.EditorFor(m => m.Permissions)

    <input type="submit" value="Create" />
}

然后将控制器方法

public ActionResult Create()
{
    var groups = db.tm_grp_group;
    var permissions = db.tm_perm_level;

    GroupPermissionVM model = new GroupPermissionVM
    {
        GroupList = new SelectList(groups, "grp_id", "grp_name"),
        Permissions = permissions.Select(p => new PermissionVM
        {
            ID = p.perm_id,
            Name = p.perm_levelname
        }
    };
    return View(model);
}

[HttpPost]
public ActionResult Create(GroupPermissionVM model)
{
    if (!ModelState.IsValid)
    {
        var groups = db.tm_grp_group;
        model.GroupList = new SelectList(groups, "grp_id", "grp_name");
        return View(model);
    }
    // map the view model to a new instance of your data model(s)
    // note: to get the ID's of the selected permissions - 
    //    var selectedPermissions = model.Permissions.Where(p => p.IsSelected).Select(p => p.ID);
    // save and redirect
}

边注︰ 我强烈建议你按照正常的命名约定

编辑

基于 OP 的注释使用单选按钮只选择一项选项,修改后的代码将

public class PermissionVM
{
  public int ID { get; set; }
  public string Name { get; set; }
}
public class GroupPermissionVM
{
  public int GroupID { get; set; }
  public int PermissionID { get; set; }
  public IEnumerable<SelectListItem> GroupList { get; set; }
  public IEnumerable<PermissionVM> Permissions { get; set; }
}

视图将 (没有单独的 EditorTemplate 需要)

@model GroupPermissionVM
....
@using (Html.BeginForm())
{
    // dropdownlist as above

    // radio buttons
    foreach (var permission in Model.Permissions)
    {
        <label>
            @Html.RadioButtonForm(m => m.PermissionID, permission.ID)
            <span>@permission.Name</span>
        </label>
    }
    <input type="submit" value="Create" />
}

POST 方法的值在 model.PermissionID 将包含 ID 的所选权限。

官方微信
官方QQ群
31647020