如何在mvc 2中的post动作中获取所选的复选框值

时间:2011-08-04 09:34:13

标签: asp.net-mvc asp.net-mvc-2 checkbox checkboxlist

我有一个从数据库填充的复选框列表,我想在post动作期间获取每个复选框列表的ID,以便我可以将其保存在db中,以下是代码: 控制器:

  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>

我想传递所选的&lt;%= item.Value%&gt;复选框列表到创建后的aqction,这样我就可以像1,2,3,4那样保存它。

4 个答案:

答案 0 :(得分:3)

如果您想在发布表单时仅传递所选复选框,请执行以下[建议]:

var myAnswers = collection["name"];

然后迭代它并保存它,或者你可以尝试这种方式

ASP.Net MVC List of Checkboxes

答案 1 :(得分:2)

非常简单。在控制器的Action方法的Parameter列表中使用FormCollection,然后为模型中的CheckBoxBox值创建一个String Array。

现在分配formvalue [“Your_CheckBoxBox_value”] .Split(new char [] {','},StringSplitOptions.RemoveEmptyEntries);

到你控制器中新创建的字符串数组........

     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 :(得分:1)

所有分组的复选框都以数组的形式返回,即您要求的1,4,8

var myAnswers = collection["name"];
// split myAnswers here if required

在你的代码中还是我错过了更大的东西?

答案 3 :(得分:0)

非常简单,在控制器的Action方法参数列表中使用FormCollection,然后为模型中的CheckBoxBox值创建一个字符串数组。

现在分配

formvalue["Your_CheckBoxBox_value"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

到Controller中新创建的String Array

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