【发布时间】:2015-06-03 00:50:30
【问题描述】:
我正在尝试使用 MVC 了解下拉列表,这似乎让我失望了。我一直在修改下面显示的代码,但无法正确处理。
What I am trying to achieve is simple - hard coding some drop down options in the controller, have them appear in the Razor rendered html and when an option is selected, that selected value is bound back to the string property in the model .
使用下面的代码,我无法从视图中访问li。
我看过其他指南,但我无法让它发挥作用,考虑到我想要实现的目标,绑定模型对我来说是最佳选择,还是 ViewBag 等会更好?
谁能告诉我哪里出错了?
型号
public class ViewModel {
public string MyOption { get; set; } }
查看
@model ViewModel
@Html.DropDownListFor(m => m.MyOption, li, "--Select--")
控制器
public ActionResult Index()
{
List<SelectListItem> li = new List<SelectListItem>();
li.Add(new SelectListItem { Text = "Option One", Value = "option1" });
li.Add(new SelectListItem { Text = "Option Two", Value = "option2" });
return View(li);
}
【问题讨论】:
标签: c# asp.net-mvc razor