我想定义一个显示标签和复选框列表的视图,用户可以更改复选框,然后回发.我在回复字典时遇到问题.也就是说,post方法的字典参数为null.
以下是GET和POST操作的操作方法:
public ActionResult MasterEdit(int id)
{
Dictionary<string, bool> kv = new Dictionary<string, bool>()
{
{"A", true},
{"B", false}
};
return View(kv);
}
[HttpPost]
public ActionResult MasterEdit(Dictionary<string, bool> kv)
{
return RedirectToAction("MasterEdit", new { id = 1 });
}
Run Code Online (Sandbox Code Playgroud)
Beliw是观点
@model System.Collections.Generic.Dictionary<string, bool>
@{
ViewBag.Title = "Edit";
}
<h2>
MasterEdit</h2>
@using (Html.BeginForm())
{
<table>
@foreach(var dic in Model)
{
<tr>
@dic.Key <input type="checkbox" name="kv" value="@dic.Value" />
</tr>
}
</table>
<input type="submit" value="Save" />
}
Run Code Online (Sandbox Code Playgroud)
任何想法都将非常感谢!