基於此代碼(自動生成的控制/查看從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是的,兩者都是正確的方式。如果您希望模型中的選定值比首先使用第二個選項更有用。 –