【问题标题】:How to keep CheckBox value after a get?获取后如何保留 CheckBox 值?
【发布时间】:2014-04-17 04:16:43
【问题描述】:

在我的 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 完成时,它会重新加载页面(当然),然后我失去了复选框状态。 我该如何处理?

【问题讨论】:

    标签: c# asp.net asp.net-mvc checkbox


    【解决方案1】:

    这都是关于视图模型的。您应该返回一个具有复选框值的视图模型,并让您的视图使用该值。如果您还返回数据,只需将数据(无论它是什么)也放入您的视图模型中。

    例子:

    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 });
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多