询问者
mvc中使用Dictionary呈现checkbox遇到的问题

问题
-
我有一个Dictionary 类型
public static class ghDictionary { public static Dictionary<int,string> getOpendict() { Dictionary<int, string> open_dict = new Dictionary<int, string>(); open_dict.Add(1, "奖金使用、分配考核情况"); open_dict.Add(2, "职工两违情况"); open_dict.Add(3, "等级考评情况"); open_dict.Add(4, "困补费救济情况"); open_dict.Add(5, "各项评先情况"); open_dict.Add(6, "竞争上岗实施方案实施过程及结果情况"); open_dict.Add(7, "民主评议车间干部、班子集体工作情况"); open_dict.Add(8, "车间认为应向职工公开的事项和涉及职工切身利益的其他事项情况"); open_dict.Add(9, "其他"); return open_dict; } }
两个 添加的动作
#region 添加 public ActionResult Create() { ViewBag.OpenItemDictiony = ghDictionary.getOpendict(); //这里呈现Dictionary return View(); } [HttpPost] [ValidateAntiForgeryToken] [ValidateInput(false)] public ActionResult Create([Bind(Include = "ID,UID,OpenItem,OpenContent,OpenFormally,OpenDate,PName,InputDateTime")] tz_OpenTransaction ot) //上面的Dictionary里的的value值 对应这里的OpenItem参数 { if (ModelState.IsValid) { if (db.tz_OpenTransactions.FirstOrDefault(o => o.UID == ot.UID && o.OpenDate == ot.OpenDate) != null) { ModelState.AddModelError("OpenDate", "您已经有相同日期的事务公开记录,请选择其他日期!"); return View(ot); } try { ot.InputDateTime = DateTime.Parse(DateTime.Now.ToString()); db.tz_OpenTransactions.Add(ot); db.SaveChanges(); TempData["Message"] = "添加成功!"; return RedirectToAction("Index"); } catch (Exception ex) { ModelState.AddModelError("OpenContent", "新增失败,请稍后重试!" + ex.Message); return View(ot); } } return View(ot); } #endregion
视图如下:
......前面的html略 <div class="col-sm-12"> <span>公开项目:</span> @foreach (var item in (Dictionary<int, string>)ViewBag.OpenItemDictiony) { if (item.Key == 1 || item.Key == 5 || item.Key == 8) { //为了排版方便,1,5,8加了样式,换行,调整padding的距离 <br /><label class="lableft"><input name="OpenItem" type="checkbox" value=@item.Key.ToString() />@item.Value </label> } else { <label><input name="OpenItem" type="checkbox" value=@item.Key.ToString() />@item.Value </label> } } @Html.ValidationMessageFor(model => model.OpenItem, "", new { @class = "text-danger" }) </div>
遇到的问题,当勾选任意一个checkbox,只能选到第一个;当我故意填写相同日期时,调试代码 return View(ot); 大概提示OpenItem异常,如何正确使用Dictionary呈现checkbox,以便正确提交,有错误发生,正确返回绑定的checkbox!!!!!!
全部回复
-
你好,
问题1: 当勾选任意一个checkbox,只能选到第一个
根据你提供的代码,我创建了一个项目,checkbox显示正常,可以多选,如果你那边只能选一个的话,我建议你检查一下是不是你使用JQuery限制了checkbox的选中项。
问题2:当我故意填写相同日期时,调试代码 return View(ot); 大概提示OpenItem异常,如何正确使用Dictionary呈现checkbox,以便正确提交,有错误发生,正确返回绑定的checkbox!
我建议你参考下面的代码: 在Create Post 方法中根据checkbox name属性获取选中的值,然后重新使用ViewBag去呈现dictionary。
public ActionResult Create2() { ViewBag.OpenItemDictiony = ghDictionary.getOpendict(); //这里呈现Dictionary return View(); } [HttpPost] [ValidateAntiForgeryToken] [ValidateInput(false)] public ActionResult Create2([Bind(Include = "ID,UID,OpenItem,OpenContent,OpenFormally,OpenDate,PName,InputDateTime")] tz_OpenTransaction ot, string[] OpenItem) //上面的Dictionary里的的value值 对应这里的OpenItem参数 { ViewBag.OpenItemDictiony = ghDictionary.getOpendict(); //这里呈现Dictionary if (ModelState.IsValid) {
视图中的代码和你的一样。
-
你好,
在MVC中提交checkbox的值时,只会提交选中的checkbox的值。
我建议你可以使用F12去看一下页面元素,确保checkbox的value值是不一样的。
另外你也可以在controller action方法中设置一个断点,看一下view中返回的值。
以下是我之前测试代码,你可以参考:
public ActionResult Create2() { ViewBag.OpenItemDictiony = ghDictionary.getOpendict(); //这里呈现Dictionary return View(); } [HttpPost] [ValidateAntiForgeryToken] [ValidateInput(false)] public ActionResult Create2([Bind(Include = "ID,UID,OpenItem,OpenContent,OpenFormally,OpenDate,PName,InputDateTime")] tz_OpenTransaction ot,
string[] OpenItem) //上面的Dictionary里的的value值 对应这里的OpenItem参数 {
//这里你可以设置一个断点查看数组的值 ViewBag.OpenItemDictiony = ghDictionary.getOpendict(); //这里呈现Dictionary if (ModelState.IsValid) { ModelState.AddModelError("OpenDate", "您已经有相同日期的事务公开记录,请选择其他日期!"); return View(ot); try { TempData["Message"] = "添加成功!"; return RedirectToAction("Index"); } catch (Exception ex) { ModelState.AddModelError("OpenContent", "新增失败,请稍后重试!" + ex.Message); return View(ot); } } return View(ot); }
视图:
<div class="col-sm-12"> <span>公开项目:</span> @foreach (var item in (Dictionary<int, string>)ViewBag.OpenItemDictiony) { if (item.Key == 1 || item.Key == 5 || item.Key == 8) { //为了排版方便,1,5,8加了样式,换行,调整padding的距离 <br /><label class="lableft"><input name="OpenItem" type="checkbox" value=@item.Key.ToString() />@item.Value </label> } else { <label><input name="OpenItem" type="checkbox" value=@item.Key.ToString() />@item.Value </label> } } @Html.ValidationMessageFor(model => model.OpenItem, "", new { @class = "text-danger" }) </div> <div class="form-group"> <div class="col-md-offset-2 col-md-10"> <input type="submit" value="Create" class="btn btn-default" /> </div> </div>
注意,如果action中数组有多个值,你可能需要去遍历每一项去做插入。