C# 如何根据ViewModel更改为值正确设置复选框?
我的视图模型包含项目列表。列表项通过C# 如何根据ViewModel更改为值正确设置复选框?,c#,asp.net-mvc,entity-framework,C#,Asp.net Mvc,Entity Framework,我的视图模型包含项目列表。列表项通过@Html.CheckBoxFor显示以供用户交互。初始设置(即检查上次在上一个会话中选择的值)以及手动选择和取消选择正确工作,并绑定回视图模型 “取消选中全部”功能最近添加到页面中它将每个列表项的值IsSelected设置为false,但页面上的所有项保持不变。 检查ModelState.Values时,相应项的AttemptedValue更改为“true\false”,而不是“false”我是否缺少任何绑定操作? 主要代码部分的快速摘要: 视图模型: pu
@Html.CheckBoxFor
显示以供用户交互。初始设置(即检查上次在上一个会话中选择的值)以及手动选择和取消选择正确工作,并绑定回视图模型
“取消选中全部”功能最近添加到页面中它将每个列表项的值IsSelected
设置为false,但页面上的所有项保持不变。
检查ModelState.Values
时,相应项的AttemptedValue
更改为“true\false”,而不是“false”我是否缺少任何绑定操作?
主要代码部分的快速摘要:
视图模型:
public class RuleDetail
{
public List<DataSourceCheckList> dataSourceCheckList { get; set; }
// many other items, methods etc...
}
以及后期控制员的行动:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Builder(int? id, ViewModels.RuleDetail rd, string ignoreChecklists, string command, int? callerId, string callerController, string callerAction)
{
//some other logic...
switch (command)
{
//bunch of other command values
case "UnselectSources":
foreach (DataSourceCheckList i in rd.dataSourceCheckList)
{
i.IsSelected = false;
}
break;
}
//update of other view model fields based on actions above
return View(rd);
}
请,您是否看到任何选项,如何确保
RuleDetail.dataSourceCheckList
中的值正确显示?问题由模型状态驱动。将ModelState.Clear()
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Builder(int? id, ViewModels.RuleDetail rd, string ignoreChecklists, string command, int? callerId, string callerController, string callerAction)
{
//some other logic...
switch (command)
{
//bunch of other command values
case "UnselectSources":
foreach (DataSourceCheckList i in rd.dataSourceCheckList)
{
i.IsSelected = false;
}
break;
}
//update of other view model fields based on actions above
ModelState.Clear();
return View(rd);
}
在这个问题上没有任何进展之后,我使用java脚本取消选中视图中的项目,并将表单提交给controller以解决其余的更新(示例:)。
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Builder(int? id, ViewModels.RuleDetail rd, string ignoreChecklists, string command, int? callerId, string callerController, string callerAction)
{
//some other logic...
switch (command)
{
//bunch of other command values
case "UnselectSources":
foreach (DataSourceCheckList i in rd.dataSourceCheckList)
{
i.IsSelected = false;
}
break;
}
//update of other view model fields based on actions above
return View(rd);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Builder(int? id, ViewModels.RuleDetail rd, string ignoreChecklists, string command, int? callerId, string callerController, string callerAction)
{
//some other logic...
switch (command)
{
//bunch of other command values
case "UnselectSources":
foreach (DataSourceCheckList i in rd.dataSourceCheckList)
{
i.IsSelected = false;
}
break;
}
//update of other view model fields based on actions above
ModelState.Clear();
return View(rd);
}