【发布时间】:2016-09-29 13:48:25
【问题描述】:
基于此代码(从 Visual Studio 自动生成的控制/视图代码):
<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" })
(也是自动生成的代码,但用于UserId)而不是@html.EditorFor...
我能够通过控制器从 db 中看到 UserName(value = Id) 的列表:
ViewBag.UserId = new SelectList(db.Users, "Id", "UserName", Test.UserId);
现在,我不希望 ViewBag 从数据库中读取,而是希望它列出 3 个不同的字符串,我将使用这些字符串让用户为 indata 选择(意思是,限制他们选择这 3 个字符串中的一个而不是写它)。
我希望它列出的字符串:
- “香蕉”
- “苹果”
- “橙色”
我该怎么做?
【问题讨论】:
标签: c# asp.net asp.net-mvc razor