2017-01-30 115 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