ASP.NET MVC DropDownListFor 模型类型为 List;
我有一个带有 List
类型模型的视图,我想在页面上放置一个下拉列表,其中包含列表中的所有字符串作为下拉列表中的项目。我是 MVC 新手,我该如何实现这一目标?
我尝试了这个:
@model List<string>
@Html.DropDownListFor(x => x)
但这引发了错误。
I have a view with a model of type List<string>
and I want to place a drop down list on the page that contains all strings from the list as items in the drop down. I am new to MVC, how would I accomplish this?
I tried this:
@model List<string>
@Html.DropDownListFor(x => x)
but that threw an error.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
要创建下拉列表,您需要两个属性:
在您的情况下您只有一个字符串列表,无法利用它来创建可用的下拉列表。
而对于数字 2,您可以让值和文本相同,您需要一个属性来绑定。您可以使用助手的弱类型版本:
其中
Foo
将是 ddl 的名称并由默认模型绑定器使用。因此,生成的标记可能看起来像这样:也就是说,下拉列表的更好的视图模型如下:
然后:
如果您想预先选择此列表中的某个选项,您需要做的就是设置此视图模型的
SelectedItemId
属性对应于Items
集合中某些元素的相应Value
。To make a dropdown list you need two properties:
In your case you only have a list of string which cannot be exploited to create a usable drop down list.
While for number 2. you could have the value and the text be the same you need a property to bind to. You could use a weakly typed version of the helper:
where
Foo
will be the name of the ddl and used by the default model binder. So the generated markup might look something like this:This being said a far better view model for a drop down list is the following:
and then:
and if you wanted to preselect some option in this list all you need to do is to set the
SelectedItemId
property of this view model to the correspondingValue
of some element in theItems
collection.如果您有一个下拉列表中想要的字符串类型列表,我会执行以下操作:
编辑:澄清,使其成为更完整的示例。
它给出了一个像这样的下拉列表:
To get the value on a controllers post;如果您使用的模型(例如 MyViewModel)具有字符串列表作为属性,因为您已指定 x =>; x.ShipNames 您只需将方法签名设置为(因为它将在模型中序列化/反序列化):
如果您只想访问帖子上的下拉列表,则签名将变为:
编辑: 根据注释已经阐明了如何访问模型集合参数中的 ShipNames 属性。
If you have a List of type string that you want in a drop down list I do the following:
EDIT: Clarified, making it a fuller example.
Which gives a drop down list like so:
To get the value on a controllers post; if you are using a model (e.g. MyViewModel) that has the List of strings as a property, because you have specified x => x.ShipNames you simply have the method signature as (because it will be serialised/deserialsed within the model):
If you just want to access the drop down list on post then the signature becomes:
EDIT: In accordance with comments have clarified how to access the ShipNames property in the model collection parameter.
我意识到这个问题很久以前就被问过,但我来这里寻找答案并且对我能找到的任何东西都不满意。我终于在这里找到了答案:
https://www.tutorialsteacher.com/mvc/htmlhelper -dropdownlist-dropdownlistfor
要从表单中获取结果,请使用 FormCollection,然后通过其模型名称提取每个单独的值:
据我所知,您为该表单指定的参数名称绝对没有区别FormCollection - 使用 Request.Form["NameFromModel"] 检索它。
不,我并没有深入了解魔法在幕后是如何运作的。我只知道它有效......
我希望这可以帮助人们避免在我开始工作之前尝试不同方法的时间。
I realize this question was asked a long time ago, but I came here looking for answers and wasn't satisfied with anything I could find. I finally found the answer here:
https://www.tutorialsteacher.com/mvc/htmlhelper-dropdownlist-dropdownlistfor
To get the results from the form, use the FormCollection and then pull each individual value out by it's model name thus:
As far as I could tell it makes absolutely no difference what argument name you give to the FormCollection - use Request.Form["NameFromModel"] to retrieve it.
No, I did not dig down to see how th4e magic works under the covers. I just know it works...
I hope this helps somebody avoid the hours I spent trying different approaches before I got it working.