C# 如何在asp.net中保持复选框的单击状态

C# 如何在asp.net中保持复选框的单击状态,c#,asp.net-mvc,asp.net-mvc-4,kendo-ui,C#,Asp.net Mvc,Asp.net Mvc 4,Kendo Ui,我在mvc4应用程序中保留复选框的状态时确实遇到了问题。我试图将其值发送到控制器逻辑,并根据给定值刷新模型中的列表,然后再将模型发送回具有新值的视图。鉴于我的复选框是“在列表中显示禁用的元素”类型的函数,我需要它能够打开和关闭。我见过很多不同的解决方案,但我似乎无法让它们发挥作用:( 以下是我的部分观点: @model MyProject.Models.HomeViewModel <div class="row-fluid"> <div class="span12"&

我在mvc4应用程序中保留复选框的状态时确实遇到了问题。我试图将其值发送到控制器逻辑,并根据给定值刷新模型中的列表,然后再将模型发送回具有新值的视图。鉴于我的复选框是“在列表中显示禁用的元素”类型的函数,我需要它能够打开和关闭。我见过很多不同的解决方案,但我似乎无法让它们发挥作用:(

以下是我的部分观点:

@model MyProject.Models.HomeViewModel

<div class="row-fluid">
    <div class="span12">
        <div class="k-block">
            <form action="~/Home/Index" name="refreshForm" method="POST">
                <p>Include disabled units: @Html.CheckBoxFor(m => m.Refresh)</p>
                <input type="submit" class="k-button" value="Refresh" />
           @* KendoUI Grid code *@
        </div>
    </div>
@model MyProject.Models.HomeViewModel
包括禁用的单位:@Html.CheckBoxFor(m=>m.Refresh)

@*肯杜伊网格代码*@
HomeViewModel:

public class HomeViewModel
{
    public List<UnitService.UnitType> UnitTypes { get; set; }
    public bool Refresh { get; set; }
}
公共类HomeViewModel
{
公共列表单元类型{get;set;}
公共bool刷新{get;set;}
}
HomeViewController将需要一些重构,但这将是一项新任务

[HttpPost]
public ActionResult Index(FormCollection formCollection, HomeViewModel model)
{
    bool showDisabled = model.Refresh;

    FilteredList = new List<UnitType>();
    Model = new HomeViewModel();
    var client = new UnitServiceClient();
    var listOfUnitsFromService = client.GetListOfUnits(showDisabled);

    if (!showDisabled)
    {
        FilteredList = listOfUnitsFromService.Where(unit => !unit.Disabled).ToList();
        Model.UnitTypes = FilteredList;

        return View(Model);
    }

    FilteredList = listOfUnitsFromService.ToList();
    Model.UnitTypes = FilteredList;

    return View(Model);
}
[HttpPost]
公共操作结果索引(FormCollection FormCollection、HomeViewModel模型)
{
bool showDisabled=model.Refresh;
FilteredList=新列表();
Model=新的HomeViewModel();
var client=新的UnitServiceClient();
var listOfUnitsFromService=client.GetListOfUnits(showDisabled);
如果(!showDisabled)
{
FilteredList=listOfUnitsFromService.Where(unit=>!unit.Disabled).ToList();
Model.UnitTypes=FilteredList;
返回视图(模型);
}
FilteredList=listOfUnitsFromService.ToList();
Model.UnitTypes=FilteredList;
返回视图(模型);
}

您将
模型
返回视图,这样将填充
模型
属性,但您的复选框值不是模型的一部分!解决方案是完全取消
表单集合
,并将复选框添加到视图模型中:

public class HomeViewModel
{
    ... // HomeViewModel's current properties go here
    public bool Refresh { get; set; }
}
在你看来:

@Html.CheckBoxFor(m => m.Refresh)
在控制器中:

[HttpPost]
public ActionResult Index(HomeViewModel model)
{
    /* Some logic here about model.Refresh */
    return View(model);
}

顺便说一句,我看不出你为什么要像现在这样将这个值添加到会话中(除非您发布的代码中有不明显的内容。

您将
模型
返回到视图,这样您的
模型
属性将被填充,但您的复选框值不是模型的一部分!解决方案是完全取消
表单集合
并将复选框添加到视图模型中:

public class HomeViewModel
{
    ... // HomeViewModel's current properties go here
    public bool Refresh { get; set; }
}
在你看来:

@Html.CheckBoxFor(m => m.Refresh)
在控制器中:

[HttpPost]
public ActionResult Index(HomeViewModel model)
{
    /* Some logic here about model.Refresh */
    return View(model);
}

顺便说一句,我看不出你为什么要像现在这样将这个值添加到会话中(除非你发布的代码中有不明显的东西。

将代码清理到尽可能小的测试用例中,这会使任何人都更容易查看它。例如“TmpList”没有在任何地方定义。现在我可以编辑这篇文章了。只需要一些时间来清理它。将代码清理到尽可能小的测试用例中,这使任何人都可以更容易地查看它。例如,“TmpList”没有在任何地方定义。现在我可以编辑这篇文章了。只需要一些时间来清理它。