[英]How do I pass a whole list data from a listbox without & with selection to controller?
我可以完美地顯示列表框上的數據,但我不知道如何在單擊提交按鈕后將列表框數據(有和沒有選擇)傳遞/發布回控制器。 有什么方法可以不使用jquery嗎?
這是我在視圖上的代碼:
<%-- I have no idea what and how to pass data back to controller --%>
<%using (Html.BeginForm()){ %>
<%=Html.ListBoxFor(m => m.AvailableGroups, Model.AvailableGroup)%>
<%=Html.ListBoxFor(m => m.AssignGroups, Model.AssignGroup)%>
<input type="submit" value="Save" />
<% } %>
在模型中,我像這樣聲明變量:
public IEnumerable<string> AvailableGroups { get; set; }
public IEnumerable<SelectListItem> AvailableGroup { get; set; }
public IEnumerable<string> AssignGroups { get; set; }
public IEnumerable<SelectListItem> AssignGroup { get; set; }
這是發布操作:
[HttpPost]
public ActionResult Update(EnrollGroup enrollgroup) { //I have no idea what kind of parameter type should I get in to this action
//Perform my code here
}
由於我無法找到返回整個列表框列表的任何方法,因此我分配了一個 jquery 函數來在提交表單之前選擇所有數據。 我更改了提交按鈕代碼下的代碼:
<input type="submit" value="Save" onclick="selectAllItem()"/>
並且它將作為模型在 AssignGroups 變量下返回到控制器。
由於您使用的是 ListBoxFor 並且它與模型中的對象有關,因此應該在您的 HTTPPost 中捕獲
下面的代碼選擇整個列表。 只需在 ajax 調用之前添加它。
$("#myList option").prop("selected", "selected");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.