这是我的控制器代码。
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
以下拉框的形式显示,但我希望它以checkboxlist的形式显示。我怎样才能做到这一点?
这是我创建的视图模型。
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; }
}
答案 0 :(得分:1)
特别是在编辑时,始终从视图模型开始,以表示您想要显示的内容(参考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
。