如何在没有&的情况下从列表框传递整个列表数据选择控制器?

时间:2014-07-22 04:02:13

标签: c# jquery asp.net-mvc model-view-controller listbox

我可以完美地显示列表框中的数据但我不知道如何在单击提交按钮后将列表框数据(带有&未选中)传递/发布回控制器。有没有办法不使用jquery呢?

以下是我在View上的代码:

<%-- 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变量下的模型返回到控制器。

2 个答案:

答案 0 :(得分:0)

由于您使用的是ListBoxFor,它与模型中的对象有关,因此应该在HTTPPost中捕获

答案 1 :(得分:0)

下面的代码选择整个列表。只需在 ajax 调用之前添加它。

$("#myList option").prop("selected", "selected");