需要有关Html.Listbox的帮助对于ASP.NET MVC
大家好
我目前正在开发一个应用程序,其中在视图的列表框中显示项目列表,然后将选定的项目发送回控制器.
我的模型如下:
Hello everyone
I am presently working on a application in which I have a display a list of items in a list box in the view and then send back the selected items to the controller.
My model is as follows:
public class Items
{
[DisplayName("Items")]
public string[] Items { get; set; }
}
当用户首次请求页面时,必须从数据库中查询项目列表并将其发送到视图.
我能够弄清楚如何将这些项收集到控制器端的ArrayList/string []中,但无法理解将视图与模型绑定以及使用Html.ListboxFor显示列表并将其发送回模型的语法.表单提交.
有人可以帮帮我吗.
谢谢.
When the user first requests the page, the list of items has to be queried from a database and sent to the view.
I am able to figure out how to collect the items into ArrayList/string[] at the controller side but am not able to understand the syntax for binding the view with the model and displaying the list using Html.ListboxFor and sending back the model on form submit.
Can someone please help me.
Thanks.
让我们说您需要在列表框控件中显示月份列表.
Let''s say you need to display months list into your list box control.
string[] months = { "January", "February", "March", "April", "May" };
您需要将其转换为SelectList,然后将其传递到您在viewdata中创建的模型中的视图.
You need to convert it into SelectList and then pass it to your view either in model you have created to in viewdata.
Dictionary<int, string> copyFromList = new Dictionary<int, string>();
SelectList list;
int i=1;
foreach (string s in months)
{
copyFromList.Add(i++, s);
}
list = new SelectList(copyFromList, "Key", "Value");
然后从此选择列表构建列表框.
HTH
Then build listbox from this select list.
HTH