2012-03-28 43 views
5

獲取選中的複選框的IDS這是我的看法:在MVC3

@foreach(var action in Model.Category.Actions) { 
<div class="action" style="margin-right: 30px;"> 
    <input type="checkbox" class="chk-act" id="@action.Id" name="actionChk" /> 
    <text>@action.Text</text> 
</div> 
    } 

和HTML DOM是像如下:

<input type="checkbox" class="chk-act" id="17" name="actionChk"> 
<input type="checkbox" class="chk-act" id="18" name="actionChk"> 
<input type="checkbox" class="chk-act" id="19" name="actionChk"> 

所以我需要檢查的ID。當我試圖讓通過形式收集值,即返回我的on一個字符串數組由長選中的複選框的:

[HttpPost] 
public ActionResult Index(FormCollection collection) { 
    var actions = collection.GetValues("actionChk"); 
return View(); 
} 

你有什麼建議嗎?

+0

由於瀏覽器將發佈輸入名稱(actionChk)因爲他們都是相同的名字,你會得到數組。爲什麼不打亂行動chk17,actionChk18等? – StuartLC 2012-03-28 12:06:43

回答

9

你應該把這些值在value參數

<input type="checkbox" class="chk-act" id="17" value="17" name="actionChk"> 
<input type="checkbox" class="chk-act" id="18" value="18" name="actionChk"> 
<input type="checkbox" class="chk-act" id="19" value="19" name="actionChk"> 

然後,從控制器,你應該有一個Ids陣列名爲actionChk

+0

簡單而有用的答案。 – Armaan 2017-03-16 11:12:44