我有一個從數據庫填充的複選框列表,我希望在post操作期間獲得每個複選框列表的ID,以便我可以將其保存在db中,是代碼: 控制器:如何獲取mvc 2中post操作中選中的複選框值
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>
我想複選框列表創建的職位aqction來傳遞選定<%= item.Value%>,這樣我可以保存它像1,2,3,4。