基于此代码(自动生成的控制/查看从Visual Studio代码)字符串的显示列表:MVC - 在DropDownList中
<div class="form-group">
@Html.LabelFor(model => model.Type, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@*Comment: Want to replace EditorFor to DropDownList("Banana, "Apple", "Orange"*@
@Html.EditorFor(model => model.Type, new { htmlAttributes = new { @class = "form-control" } })*@
@Html.ValidationMessageFor(model => model.Type, "", new { @class = "text-danger" })
</div>
</div>
我想补充相似喜欢的东西:
@Html.DropDownList("UserId", null, htmlAttributes: new { @class = "form-control" })
代替
@html.EditorFor...
(也自动生成的代码,但对于UserId
)我能看到的UserName
列表(值= Id
)的F通过控制器ROM DB:
ViewBag.UserId = new SelectList(db.Users, "Id", "UserName", Test.UserId);
现在,我不希望这样ViewBag从数据库中读取,而不是我希望它列出我将用它来让用户选择了INDATA(意思3个不同的字符串,限制他们选择这3个字符串中的一个来代替它)。
字符串我希望它列出:
- “香蕉”
- “苹果”
- “橙色”
我该怎么办呢?
感谢它运作良好!只是一个问题,这在工作领域仍然是一种正确的方式(例如平均安全性)还是应该考虑其他解决方案? – Nyprez
@Nyprez是的,两者都是正确的方式。如果您希望模型中的选定值比首先使用第二个选项更有用。 –