How to get the selected checkbox value in post action in mvc 2

别来无恙 提交于 2019-12-24 07:38:11

问题


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 .


回答1:


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();
    }
}



回答2:


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




回答3:


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?




回答4:


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();
    }
}


来源:https://stackoverflow.com/questions/6939097/how-to-get-the-selected-checkbox-value-in-post-action-in-mvc-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!