我很努力從我在兩個組之間進行排序的表單中獲取數據。表格看起來像這樣。 正如你所看到的,取得動作是偉大的工作,整個頁面的工作就好了..這裏是.cshtml視圖沒有從ListBoxFor和MultiSelectList在MVC中發佈表單數據
@model InfoKeeper.Models.GroupManageVewModel
@{
ViewBag.Title = "ManageGroup";
}
<h2>ManageGroup</h2>
@using (Html.BeginForm("ManageGroup","GroupManager", FormMethod.Post))
{
<div class="nav-block">
<div class="row">
<div class="col-xs-3 col-xs-pull-1 col-xs-offset-2">
<p>Available Groups</p>
@Html.HiddenFor(m => m.GroupID)
@Html.HiddenFor(m => m.CustomerID)
@Html.ListBoxFor(m => m.ResultAvailable, Model.AvailableDepartmentsSL, new { @Name = "from[]", @id = "multiselect", @class = "form-control", @size = "8", @multiple = "multiple", @style = "min-width: 350px;margin-right:50px;" })
</div>
<div class="col-xs-2" style="padding-top:25px;">
<button type="button" id="multiselect_rightAll" class="btn btn-block"><i class="glyphicon glyphicon-forward"></i></button>
<button type="button" id="multiselect_rightSelected" class="btn btn-block"><i class="glyphicon glyphicon-chevron-right"></i></button>
<button type="button" id="multiselect_leftSelected" class="btn btn-block"><i class="glyphicon glyphicon-chevron-left"></i></button>
<button type="button" id="multiselect_leftAll" class="btn btn-block"><i class="glyphicon glyphicon-backward"></i></button>
<input type="submit" value="save" class="btn btn-block btn-success">
</div>
<div class="col-xs-3">
<p>Departments in Group</p>
@Html.ListBoxFor(m => m.ResultInGroup, Model.DepartmentsInGroupSL, new { @Name = "to[]", @id = "multiselect_to", @class = "form-control", @size = "8", @multiple = "multiple", @style = "min-width: 350px" })
</div>
</div>
</div>
}
而且,這裏是控制器...
public ActionResult ManageGroup(int GroupID, int CustomerID)
{
GroupRepo repo = new GroupRepo();
var model = repo.ManageGroup(GroupID, CustomerID);
model.AvailableDepartmentsSL = new MultiSelectList(model.AvailableDepartments, "Key", "Value");
model.DepartmentsInGroupSL = new MultiSelectList(model.DepartmentsInGroup, "Key", "Value");
return View(model);
}
和模型...
public class GroupManageVewModel
{
public MultiSelectList AvailableDepartmentsSL { get; set; }
public MultiSelectList DepartmentsInGroupSL { get; set; }
public Dictionary<int, string> DepartmentsInGroup { get; set; }
public Dictionary<int, string> AvailableDepartments { get; set; }
public int GroupID { get; set; }
public int CustomerID { get; set; }
public string GroupName { get; set; }
public IEnumerable<int> ResultAvailable { get; set; }
public IEnumerable<int> ResultInGroup { get; set; }
}
但是,當我提交表單...這是我得到的帖子...
因此,表格發佈東西 ... HiddenFors。但由於什麼原因我的ResultAvailable和ResultInGroup爲空...請幫助!
使用'HtmlHelper'方法時永遠不要改變'name'屬性。你沒有名爲'form'和'to'的屬性(並且添加'new {multiple =「multiple」}'是毫無意義的 - 'ListBoxFor()'方法已經添加了這個 –
請看下面我添加的答案! ,SO。 – SentOnLine