2011-08-04 72 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(); 
    } 
} 
相關問題