简体   繁体   English

正确使用@ Html.DropDownListfor

[英]Proper usage of @Html.DropDownListfor

I have this textbox : 我有这个文本框:

@Html.EditorFor(model => model.Type)

Model.Type is a string. Model.Type是一个字符串。 I want to change this to DropDownList for that type. 我想将此类型更改为DropDownList。 I want to handle list elements on View, I don't want to use a class for that. 我想在View上处理列表元素,我不想为此使用类。 My failed attempt is this : 我失败的尝试是这样的:

@Html.DropDownListFor(model=>model.Type, 
 new List<SelectListItem>
{
  new SelectListItem {"Horror"},
  new SelectListItem {"Fiction"},
  new SelectListItem {"Romance"},
})

How can I use DropDownListFor correctly in this case? 在这种情况下,如何正确使用DropDownListFor? Thanks. 谢谢。

Almost there. 快好了。 This 这个

new SelectListItem {"Horror"}

makes no sense. 没有意义。

it should be 它应该是

new SelectListItem { Text = "Horror", Value = "Horror" }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM