2011-12-08 135 views
2

我想下拉绑定在MVC模型中,但我得到的错误我的代码如下:在MVC绑定下拉2.0

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; 
} 

任何机构可以说我哪里错了?

+0

你会得到什么错误? – Ruben

+0

在运行时它没有绑定我的下拉列表 –

+0

请参见我的回答[类似文章](http://stackoverflow.com/questions/8383105/mvc-3-dropdownlist-or-dropdownlistfor-cant-save-values-in- controller-post/8387828#8387828) –

回答

2

这也可以工作:

<select name="sdfsdf"> 
    @foreach selectOption in ViewBag['SelectOptions'] 
     <option value="@selectOption.Value">@selectOption.Text</option> 
    @endforeach 
</select> 

只是爱在viewbag把一切都在控制器 (这个例子是在剃刀)

随着ASPX这将是这样的,我认为.. 。但它已经有一段时间,因为我写的任何ASPX,所以我可能会错过一些但是这是主要的想法

<select name="sdfsdf"> 
    <% foreach(var p in products) { %> 
     <option value="<% p.Value %>"> <% p.Text %> </option> 
    <% } %> 
</select> 
+0

很好的MVC 3.0解决方案。 MVC 2.0不支持Razor –

+0

是的,但你可以翻译它的课程 – Ruben

+0

使用ViewBag或ViewData对于应用程序是好的,因为它是松散绑定的数据? –