如何在获取后保持CheckBox值
本文关键字:CheckBox 获取 | 更新日期: 2023-09-27 17:58:36
在我的ASP中。NET MVC 5网站我有这种情况:
我有一个GridView,我可以只获取默认行或所有行(包括已删除的行)。我试图使用视图功能区中名为"cbxGetAll"的复选框来控制它。
所以,这是我的脚本:
<script>
function OnCommandExecuted(s, e) {
if (e.item.name == "cbxGetAll") {
if (e.parameter) {
window.location.href = '@Url.Action("Index",new {getAll = true})';
return;
} else {
window.location.href = '@Url.Action("Index",new {getAll = false})';
return;
}
}
</script>
我的行动:
public ActionResult Index(bool? getAll)
{
if (getAll != null && (bool) getAll)
{
//Return Without Filter
}
else
{
//Return With Filter
}
}
我更改了URL中的getAll参数,它运行良好。但问题是,当ActionResult完成时,它会重新加载页面(当然),然后我就丢失了复选框状态。我该怎么处理?
这一切都与视图模型有关。您应该返回一个具有复选框值的视图模型,并让视图使用该值。如果您也在返回数据,只需将数据(无论是什么)也放置在视图模型中即可。
示例:
public class MyViewModel
{
public bool GetAll { get; set; }
public SomeDataModel[] MyData { get; set; }
}
public ActionResult Index(bool? getAll)
{
SomeDataModel[] data;
if (getAll != null && (bool) getAll)
{
var data = GetSomeData(true);
}
else
{
var data = GetSomeData(false);
}
return View(new MyViewModel() { MyData = data, GetAll = getAll == true });
}