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 .
if you want to pass only the selected checkboxes when you post the form then do the following [as suggested]:
var myAnswers = collection["name"];
and then iterate through it and save it or you can try this way to
ASP.Net MVC List of Checkboxes
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 ........
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
model.CheckBoxValues=collection["Your_CheckBox_valueOnView"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
return RedirectToAction("Index");
}
catch
{
return View();
}
}
all grouped checkboxes are returned as an array i.e 1,4,8 you just request
var myAnswers = collection["name"];
// split myAnswers here if required
in your code or am i missing something bigger here?
It's 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
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
model.CheckBoxValues=collection["Your_CheckBox_valueOnView"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
return RedirectToAction("Index");
}
catch
{
return View();
}
}
精彩评论