2011-08-04 36 views
1

我有一个从数据库填充的复选框列表,我希望在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。

回答

2

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

现在分配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(); 
    } 
} 
1

所有分组复选框被作为数组返回即1,4,8你只是要求

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

在你的代码还是我失去了一些东西更大吗?

3

,如果你想在您发布窗体然后执行以下操作[作为建议]通过只对选定的复选框:

var myAnswers = collection["name"]; 

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

ASP.Net MVC List of Checkboxes

0

这很简单,使用FormCollection行动方法的参数列表在你的控制器,然后创建一个字符串数组为您的模型中的CheckBoxBox值。

现在指定

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(); 
    } 
} 
相关问题