我试图在 MVC 模型中绑定(bind)下拉菜单,但出现错误我的代码如下:
private IEnumerable<System.Web.Mvc.SelectListItem> _property;
public IEnumerable<System.Web.Mvc.SelectListItem> TestPapers
{
get
{
Controller _controller = new Controller();
_property = _controller.BindTestPaperDropdown(string.Empty);
return _property;
}
set
{
_property = value;
}
谁能说我哪里错了?
最佳答案
这也可以工作:
<select name="sdfsdf">
@foreach selectOption in ViewBag['SelectOptions']
<option value="@selectOption.Value">@selectOption.Text</option>
@endforeach
</select>
只要把所有东西都放在 Controller 的 viewbag 里 (示例在 Razor 中)
对于 aspx,我想它会是这样的……但是我已经有一段时间没有写任何 aspx 了,所以我可能会遗漏一些东西,但这是主要思想
<select name="sdfsdf">
<% foreach(var p in products) { %>
<option value="<% p.Value %>"> <% p.Text %> </option>
<% } %>
</select>
关于c# - MVC 2.0 中的绑定(bind)下拉列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8429433/