1

重构我的 MVC 页面以获得更好的设计,就这样吧。

模型:

public class MyModel
{
    public IEnumerable<SelectListItem> SpeciesDropDown {get; set;}
}

控制器

 [HttpGet]
    public ActionResult Index()
    {
    MyModel model = new MyModel()
    {
    SpeciesDropDown = StaticClass.GetSpeciesSelectList()
    }

GetSpeciesSelectList 返回值列表的位置(我知道它可以工作,因为它与 viewdata 一起工作。现在视图可以工作,但有一个错误

 @model MyModel
    Species: @Html.DropDownList("SpeciesDropDown", 
    new SelectList(ViewData.Model.SpeciesDropDown))

它吐出一堆System.Web.Mvc.SelectListItem. 有什么建议么?谢谢

4

1 回答 1

1

代替

@Html.DropDownList("SpeciesDropDown", 
new SelectList(ViewData.Model.SpeciesDropDown))

使用时会发生什么

@Html.DropDownList("SpeciesDropDown", ViewData.Model.SpeciesDropDown)

也看看这个答案:

Asp.Net MVC 2 下拉显示 System.Web.MVC.SelectListItem

于 2012-10-03T18:21:47.080 回答