[英]@Html.ListBoxFor usage and creation questions
我有一個帶有2個列表框的網頁( list1,list2 ),我可以將項目從一個列表框移到另一個列表框,然后再移回另一個列表框。 當我單擊提交按鈕時,我想將list2中的所有項目值返回給httppost方法。
我有3個問題:
模型:
public class TestModel
{
public List<SelectListItem> OptionList { get; set; }
public List<SelectListItem> SelectedOptionList { get; set; }
public string[] SelectedOptions { get; set; }
}
控制器:
private TestModel testModel = new TestModel();
public ActionResult TestPage()
{
ViewBag.Message = "Test Page";
testModel.OptionList = new List<SelectListItem>();
for (int i = 1; i <= 100; i++)
{
SelectListItem item = new SelectListItem();
item.Text = "Option " + i.ToString();
item.Value = item.Text;
testModel.OptionList.Add(item);
}
return View("TestView", testModel);
}
[HttpPost]
public ActionResult TestPage(TestModel formModel)
{
testModel.SelectedOptionList = formModel.SelectedOptionList;
testModel.SelectedOptions = formModel.SelectedOptions;
//do something
return RedirectToAction("TestPage");
}
視圖:
@model EngineeringDataAnalysisGui.Models.TestModel
@{
ViewBag.Title = @ViewBag.Message;
}
@section Subtitle
{
@ViewBag.Message
}
<table width="100%">
@using (Html.BeginForm("TestPage", "RetrieveData", FormMethod.Post))
{
<tr>
<td>Event List:</td>
<td>@Html.ListBox("lstEventSelection", Model.OptionList, new { id = "list1", Multiple = "multiple", Size = 15, style = "width: 100%;" })</td>
</tr>
<tr>
<td></td>
<td>
<table style="text-align:center; width: 100%">
<tr>
<td id="buttonCol">
<input type="button" id="btnAddAllEvent" title="Add All Events" onclick="moveAllOptions('list1','list2', true)" />
<input type="button" id="btnAddEvent" title="Add Selected Events" onclick="moveSelectedOptions('list1','list2', true)" />
</td>
<td id="buttonCol">
<input type="button" id="btnRemoveEvent" title="Remove Selected Events" onclick="moveSelectedOptions('list2','list1', true)" />
<input type="button" id="btnRemoveAllEvent" title="Remove All Events" onclick="moveAllOptions('list2','list1', true)" />
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>Selected Event List:</td>
@*Not sure how to add the listbox*@
<td>@*Wrong*@@Html.ListBoxFor(x => x.SelectedOptions, Model.SelectedOptionList, new { id = "list2", Multiple = "multiple", Size = 15, style = "width: 100%;" })</td>
</tr>
<tr>
<td><input type="submit" value="submit" /></td>
</tr>
}
</table>
1)如何確定所選列表的屬性?
@Html.ListBoxFor(x => x.SelectedOptions, new MultiSelectList(Model.SelectedOptionList), new { id = "list2", Multiple = "multiple", Size = 15, style = "width: 100%;" })
2)如何有一個初始的空列表(對於list2)? list1已填充項目。 在控制器中設置一個空列表testModel.SelectedOptionList = new List<SelectListItem>();
3)單擊提交按鈕時如何將所有list2項目返回模型
提交之前,可能需要將list2中的所有項目設置為選中狀態(使用jquery)。 list2項目中的所有選定項目將在提交時綁定到TestModel.SelectedOptions。
請環顧四周,SO中已經有很多與Html.ListBoxFor相關的答案。 例如,在ListBoxFor中選擇值的挑戰
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.