0
A
回答
1
對於你的情況,你應該使用其他的輔助工作時 - @Html.ListBoxFor
它應該產生select
元素與multiple
屬性。
//note that i use MaintanceDevices property
@Html.ListBoxFor(x => x.MaintanceDevices, new SelectList(Model.Devises, "ID", "Description"), new { @class = "multiselect form-control"})
此外,不要在助手中設置id
屬性。這是更好地在你的視圖模型創建另一個屬性:
public List<int> MaintanceDevices { get; set; }
填充它的控制器和MVC自動生成正確的標記爲您select
元素綁定在表單POST。
1
您的模型中的設備屬性應該是一個Ids列表(其中是一個簡單的類型,如int或字符串),而不是設備模型列表(因爲您在Helper中使用new SelectList(Model.Devices, "ID", "Description")
,所以我看到Model。設備是複雜對象的集合)
所以,你的模型應該是這樣的:
public List<Device> AvailableDevices { get;set; }
public List<string> Devices { get;set; }
和助手應該是
@Html.ListBoxFor(m=>m.Devices,new SelectList(Model.AvailableDevices , "ID", "Description"))
或
@Html.DropDownListFor(m=>m.Devices,new SelectList(Model.AvailableDevices , "ID", "Description", new {multiple="multiple"})
後的行動應該得到無論是List<string>
作爲參數或完整型號:
[HttpPost]
public ActionResult Submit(List<string> devices)
或
[HttpPost]
public ActionResult Submit(YourModel model)
//where YourModel model is the same type that you are using to render your view
1
在這種情況下,我會做的視圖模型
內以下public string Devices { get; set; }
List<int> innerList;
public List<int> List
{
get
{
if (this.innerList == null)
{
if (string.IsNullOrEmpty(this.Devices))
{
this.innerList = this.Devices.Split(',').Select(x => int.Parse(x)).ToList();
}
else
{
this.innerList = new List<int>();
}
}
return this.innerList;
}
}
其中Devices
是下拉菜單的綁定屬性,它將返回由,
分隔的所有項目。 當您嘗試訪問List
時,它將分離項目並將其作爲List<int>
返回。
而且我也解析到int
,因爲通常我看到int
的作爲ID的
,但我期待着一個更好的選擇。
PS
我這樣做與Select2
相關問題
- 1. C++是否可以將多個方法綁定在一起?
- 2. 是否可以將選擇選項更改綁定到函數?
- 3. 是否可以模擬Activator.GetObject()?
- 4. 是否可以模擬iframe?
- 5. 是否可以在模板綁定中添加可選的綁定參數?
- 6. WSDL綁定是否可選?
- 7. 是否可以綁定到淘汰賽屬性選項datepickerOptions
- 8. 是否可以綁定Grid.RowProperty。
- 9. 是否可以模擬iPhone和/或模擬器的多個音頻輸出?
- 10. 是否可以綁定多個控件來過濾一個NSArrayController?
- 11. 是否可以測試選擇框中有多少個選項?
- 12. 是否可以編寫一個通用重新綁定模板?
- 13. 是否可以在C/C++中模擬Go界面?
- 14. 是否可以在Docker中綁定多個文件夾?
- 15. xaml中的元素是否可以綁定到多個路徑?
- 16. 是否可以 - 模擬此方法?
- 17. 是否可以在UIWebView上模擬UITouch?
- 18. 是否可以在IE8中模擬Object.getOwnPropertyNames
- 19. 是否可以模擬文件列表?
- 20. 是否可以模擬FTDI USB設備?
- 21. 是否可以通過多個參數進行optparse-applicative選項?
- 22. 是否可以將liistbox選定值綁定到mediaelement源wpf
- 23. 是否可以在模擬中設置模擬?
- 24. 是否可以在iPhone模擬器上模擬電話?
- 25. 「是」 C#多個選項
- 26. DNN - 模型綁定是否可能?
- 27. 是否可以使用c#來模擬ms-access的功能?
- 28. 是否可以在C++類聲明中模擬類名?
- 29. 是否有可能綁定選擇選項上選擇而不是更改
- 30. 我可以模擬SqlConnection.BeginTransaction c#?
可能的[Html。DropdownListFor選擇的值沒有設置](http://stackoverflow.com/questions/19476530/html-dropdownlistfor-selected-value-not-being-set) –