【问题标题】:ASP MVC 5 - keep @html.checkbox state after page reload after clicking "submit button"ASP MVC 5 - 单击“提交按钮”后页面重新加载后保持@html.checkbox 状态
【发布时间】:2017-06-15 07:39:15
【问题描述】:

我创建了一个页面,我可以通过实体框架搜索商店详细信息 我在表格中添加了一列复选框。 通过搜索按钮提交后,我想保留复选框“checked=True”。

实现这一目标的推荐方法是什么?

我尝试了以下方法,但单击提交后复选框被“取消选中” 1.https://www.sitepoint.com/quick-tip-persist-checkbox-checked-state-after-page-reload/

如下图:

@using (Html.BeginForm())
{
<p>
    Find by name: @Html.TextBox("SearchString") 
    <input type="submit"  name ="StoreIndexButton" value="Search" />
</p>
}
<table class="table" id="displayresult">

@foreach (var item in Model) {
<tr>
    <td>
        @Html.DisplayFor(modelItem => item.name)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.market)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.status)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.oper)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.operator)
    </td>
    <td>
        @Html.ActionLink("Details", "Details", new { id=item.store1 })
    </td>
    <td>
        @Html.CheckBox("selected",new { value = item.store1,     id="selected"+item.store1.ToString() })
    </td>
</tr>
}

</table>

控制器如下:

public ActionResult Index(string StoreIndexButton,string searchString)
{
    var AR_stores = (from m in db.stores
                     select m).Take(10);
    string[] selectedList = Request.Form.GetValues("selected");
    if (!String.IsNullOrEmpty(searchString) && StoreIndexButton =="Search")
    {
         AR_stores = (from m in db.stores
                      select m).Where(s =>   s.name.Contains(searchString));
    }
    return View(AR_stores);    
}

型号如下:

using System;
using System.Collections.Generic;

public partial class store
{
    public int store1 { get; set; }
    public string name { get; set; }
    public string market { get; set; }
    public string status { get; set; }
    public string oper { get; set; }
    public string operator { get; set; } 
}

【问题讨论】:

  • 你找到答案了吗?

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


【解决方案1】:

我会这样:

在部分类上添加标志参数。

public string checkStatus {get; set;}

默认情况下,这将为 false,并且不会选择任何选项。

提交函数后,你有选中的项目列表,你可以将它们设置为选中(checkStatus 为 true)。

【讨论】:

    【解决方案2】:

    MVC 没有像 ASP.Net WebForms 那样的 ViewState(不确定 viewstate 是一个特性还是缺点)。因此,您必须使用隐藏字段或变量来保存复选框的状态。你可以用 jquery 和 ajax 试试这个

    【讨论】:

      猜你喜欢
      • 2018-10-15
      • 2021-12-08
      • 2015-07-29
      • 2017-07-21
      • 1970-01-01
      • 1970-01-01
      • 2015-11-23
      • 2017-06-25
      • 1970-01-01
      相关资源
      最近更新 更多