Asp.net mvc 如何根据mvc中的db值获取要检查的复选框列表
我在编辑产品页面中有一个复选框列表,我希望复选框列表 根据db值进行检查 例如,我将db中的值保存为1,2,3 因此,应选中值为1、值为2、值为3的复选框列表 代码如下: 控制器:Asp.net mvc 如何根据mvc中的db值获取要检查的复选框列表,asp.net-mvc,asp.net-mvc-2,checkbox,Asp.net Mvc,Asp.net Mvc 2,Checkbox,我在编辑产品页面中有一个复选框列表,我希望复选框列表 根据db值进行检查 例如,我将db中的值保存为1,2,3 因此,应选中值为1、值为2、值为3的复选框列表 代码如下: 控制器: public ActionResult Edit(int id) { ITrackdayRepository trackdayResp = new TrackdayRepository(); IQueryable<Object> getAllproducts =
public ActionResult Edit(int id)
{
ITrackdayRepository trackdayResp = new TrackdayRepository();
IQueryable<Object> getAllproducts = trackdayResp.GetProductsSelectlist();
ViewData["products"] = new SelectList(getAllproducts.ToList(), "productID", "Name");//all events for checkboxlist
IVoucherRepository voucherResp = new VoucherRepository();
Voucher voucher = voucherResp.GetVoucher(id);
return View(voucher);
}
//
// POST: /Admin/Voucher/Edit/5
[HttpPost]
public ActionResult Edit(int id, FormCollection collection)
{
try
{
// TODO: update the selected checkbox nd do db insert
return RedirectToAction("Index");
}
catch
{
return View();
}
}
public ActionResult编辑(int-id)
{
ITrackdayRepository trackdayResp=新的TrackdayRepository();
IQueryable getAllproducts=trackdayResp.GetProductsSelectlist();
ViewData[“products”]=新建SelectList(getAllproducts.ToList(),“productID”,“Name”);//checkboxlist的所有事件
IVoucherRepository voucherResp=新的VoucherRepository();
凭证凭证=凭证响应获取凭证(id);
返回视图(凭证);
}
//
//POST:/Admin/凭证/编辑/5
[HttpPost]
公共操作结果编辑(int id,FormCollection集合)
{
尝试
{
//TODO:更新所选复选框并插入数据库
返回操作(“索引”);
}
抓住
{
返回视图();
}
}
视图:
产品
编辑:
<td>
<% foreach (var item in (SelectList)ViewData["events"]) { %>
<%var test = ViewData["arrays"]; %>
<%string checkString = test.ToString().Contains(item.Value) ? "checked=\"checked\"":string.Empty; %>
<input type="checkbox" name="Name" value="<%=item.Value %>" checked="<%=checkString %>" />
<label for="<%=item.Value%>"><%=item.Text%></label>
<br />
<% } %>
</td>
要选中复选框,需要在输入元素上设置checked属性
<input type="checkbox" name="Name" value="<%=item.Value %>" checked="checked" />
编辑:
假设始终希望选中1、2或3,则可以执行以下操作:
<% var valuesToCheck = new List<int>() { 1, 2, 3 };
foreach (var item in (SelectList)ViewData["products"]) {
string checkedString = valuesToCheck.Contains(item.productID) ? "checked=/"checked/" : string.empty;
%>
<input type="checkbox" name="Name" value="<%=item.Value %>" <%=checkedString%> />
<label for="<%=item.Value%>"><%=item.Text%></label>
<br />
<% } %>
我知道,但我不想选中所有复选框,只想选中值与db匹配的复选框values@MrA-哪个对象/属性包含db值,哪个包含所有其他值?item.productID包含类似于1,2,3的值,因此我希望选中值为1和2的复选框3@Mr那么你总是要检查1,2,3吗?如果item.productID可以是其他值,您如何知道要检查哪些值?不,这只是一个示例,这些值可以更改item.productID可以有5、4、6等等这些是保存在db中的ID,我想将这些值与逗号分开并保存在数组中,然后以某种方式传递给复选框进行检查,这可能吗
<% var valuesToCheck = new List<int>() { 1, 2, 3 };
foreach (var item in (SelectList)ViewData["products"]) {
string checkedString = valuesToCheck.Contains(item.productID) ? "checked=/"checked/" : string.empty;
%>
<input type="checkbox" name="Name" value="<%=item.Value %>" <%=checkedString%> />
<label for="<%=item.Value%>"><%=item.Text%></label>
<br />
<% } %>