如何在 mvc 2 的后期操作中获取选定的复选框值
我有一个从数据库填充的复选框列表,我想在后期操作期间获取每个复选框列表的 ID,以便我可以将其保存在数据库中,下面是代码: 控制器:
public ActionResult Create()
{
ITrackdayRepository trackdayResp = new TrackdayRepository();
IQueryable<Object> getAllEvents = trackdayResp.GetEventsSelectlist();
var m = new SelectList(getAllEvents,"ID","Name");
ViewData["events"] = new SelectList(getAllEvents.ToList(), "EventID","Date");
return View();
}
//
// POST: /Admin/Voucher/Create
[HttpPost]
public ActionResult Create(FormCollection collection)
{
try
{
// Get all the selected checkboxlist, do db insertion
return RedirectToAction("Index");
}
catch
{
return View();
}
}
视图
<label>Events</label>
</td>
<td>
<% foreach (var item in (SelectList)ViewData["events"]) { %>
<input type="checkbox" name="Name" value="<%=item.Value %>" />
<label for="<%=item.Value%>"><%=item.Text%></label>
<br />
<% } %>
</td>
我想传递选定的 <%=item.Value %>将复选框列表添加到 create 的 post aqction 中,以便我可以像 1,2,3,4 一样保存它。
I have got a checkbox list populated from database , I want to get the ID of each checkbox list during post action so that I can save that in the db , Below is the Code :
Controller:
public ActionResult Create()
{
ITrackdayRepository trackdayResp = new TrackdayRepository();
IQueryable<Object> getAllEvents = trackdayResp.GetEventsSelectlist();
var m = new SelectList(getAllEvents,"ID","Name");
ViewData["events"] = new SelectList(getAllEvents.ToList(), "EventID","Date");
return View();
}
//
// POST: /Admin/Voucher/Create
[HttpPost]
public ActionResult Create(FormCollection collection)
{
try
{
// Get all the selected checkboxlist, do db insertion
return RedirectToAction("Index");
}
catch
{
return View();
}
}
View
<label>Events</label>
</td>
<td>
<% foreach (var item in (SelectList)ViewData["events"]) { %>
<input type="checkbox" name="Name" value="<%=item.Value %>" />
<label for="<%=item.Value%>"><%=item.Text%></label>
<br />
<% } %>
</td>
I want to pass selected <%=item.Value %> of the checkbox list to the post aqction of create , so that i can save it like 1,2,3,4 .
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您想在发布表单时仅传递选定的复选框,请执行以下操作[按照建议]:
然后迭代并保存它,或者您可以尝试这种方式
ASP.Net MVC 复选框列表
if you want to pass only the selected checkboxes when you post the form then do the following [as suggested]:
and then iterate through it and save it or you can try this way to
ASP.Net MVC List of Checkboxes
非常简单。在控制器中 Action 方法的参数列表中使用 FormCollection,然后为模型中的 CheckBoxBox 值创建一个字符串数组。
现在分配 formvalue["Your_CheckBoxBox_value"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
到控制器中新创建的字符串数组........
Its very simple . Use FormCollection in your Parameter list of Action method in your controller and then create a String Array for your CheckBoxBox values in your model .
Now Assign formvalue["Your_CheckBoxBox_value"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
to your newly created String Array in your Controller ........
所有分组的复选框都作为数组返回,即您只是
在代码中请求的 1,4,8 还是我在这里遗漏了更大的东西?
all grouped checkboxes are returned as an array i.e 1,4,8 you just request
in your code or am i missing something bigger here?
非常简单,在控制器的 Action 方法的参数列表中使用
FormCollection
,然后为模型中的CheckBoxBox
值创建一个字符串数组。现在分配
给控制器中新创建的字符串数组
It's very simple, use
FormCollection
in your Parameter list of Action method in your controller and then create a String Array for yourCheckBoxBox
values in your model.Now Assign
to your newly created String Array in your Controller