2017-01-30 114 views
1

我创建了一个网页,我可以通过实体框架 搜索商店细节我在表中添加复选框列。 我希望在通过搜索按钮提交后保留“checked = True”复选框。ASP MVC 5 - 保持页面重载后@ html.checkbox状态点击“提交按钮”后

什么是推荐的方式来实现呢?

我尝试以下方法,但该复选框得到 “选中” 我点击提交后

查看如下:

@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; } 
} 
+0

你有没有找到这个答案? – whisk

回答

0

我会这样:

在部分类添加一个标志参数。

public string checkStatus {get; set;} 

默认情况下,这将是错误的,不会选择任何选项。

提交后的功能,您选择的产品清单,并作为选择,你可以将它们(的checkStatus将是真实的)。

0

MVC没有ViewState的像ASP.Net Web窗体(不知道视图状态是一个功能或回缩)。所以你必须使用隐藏字段或变量来保存复选框的状态。你可以试试jQuery和AJAX