这是我的控制器代码。
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
中以下拉框的形式显示,但我希望它以复选框列表的形式显示。我该如何实现?这是我创建的视图模型。
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; }
}
最佳答案
特别是在编辑时,请始终从视图模型开始以代表您要显示的内容(请参阅What is ViewModel in 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; }
}
然后为
EditorTemplate
创建一个PermissionVM
。在/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
。