我创建了一个网页,我可以通过实体框架 搜索商店细节我在表中添加复选框列。 我希望在通过搜索按钮提交后保留“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