我創建了一個網頁,我可以通過實體框架 搜索商店細節我在表中添加複選框列。 我希望在通過搜索按鈕提交後保留「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; }
}
你有沒有找到這個答案? – whisk