如何获得MVC 2后行动选中的复选框值 [英] How to get the selected checkbox value in post action in mvc 2

查看:128
本文介绍了如何获得MVC 2后行动选中的复选框值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已得到数据库填充一个复选框列表,我想POST操作过程中获取每个复选框列表的ID,这样我可以保存在数据库中,下面是code:
控制器:

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

查看

<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%复选框列表创建的职位aqction的>,所以我能救它像1,2,3,4

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 .

推荐答案

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

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 .

现在指定formvalue [Your_CheckBoxBox_value]斯普利特(新的char [] {','},StringSplitOptions.RemoveEmptyEntries);

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

这篇关于如何获得MVC 2后行动选中的复选框值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆