在ASP.NET MVC 3中填充下拉列表

时间:2012-01-31 20:36:29

标签: asp.net-mvc-3 razor drop-down-menu

我需要填充ASP.NET MVC 3中的下拉列表。我希望得到以下问题的答案:

  1. 我有什么选择。我的意思是@ Html.DropDownList和@ Html.DropDownListFor之间的区别。还有其他选择吗?
  2. 我有以下课程/代码。假设我只需要使用下面的类显示这个下拉列表,我需要什么样的剃刀代码/ HTML语法(我已经包含了我正在尝试的内容)。

    public class SearchCriterion
    {
      public int Id { get; set; }
    
      public string Text { get; set; }
    
      public string Value { get; set; }
    }
    
    public class SearchCriteriaViewModel
    {
     public IEnumerable<SelectListItem> SearchCriteria { get; set; }
    }
    
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            IList<System.Web.WebPages.Html.SelectListItem> searchCriteriaSelectList = 
    new List<System.Web.WebPages.Html.SelectListItem>();
    
            SearchCriteriaViewModel model = new SearchCriteriaViewModel();
    
            //Some code to populate searchCriteriaSelectList goes here....
    
            model.SearchCriteria = searchCriteriaSelectList;
    
            return View(model);
        }
    }
    
    //Code for Index.cshtml
    
    @model SearchCriteriaViewModel
    
    @{
        ViewBag.Title = "Index";
    }
    
    <p>
        @Html.DropDownListFor(model => model.SearchCriteria, 
               new SelectList(SearchCriteria, "Value", "Text"))
      </p>
    

2 个答案:

答案 0 :(得分:2)

lambda =>的右侧必须是一个简单类型而不是复杂类型修改代码

public class SearchCriteriaViewModel
{
 public int Id { get; set; }
 public IEnumerable<SearchCriterion> SearchCriteria { get; set; }
}

public class SearchCriterion
{     
  public string Text { get; set; }
  public string Value { get; set; }
}

控制器看起来像

public ActionResult Index()
{
    //fill the select list 
    IEnumerable<SearchCriteria> searchCriteriaSelectList = 
             Enumerable.Range(1,5).Select(x=>new SearchCriteria{                 
                Text= x.ToString(),
                Value=ToString(),
            });
    SearchCriteriaViewModel model = new SearchCriteriaViewModel();
    //Some code to populate searchCriteriaSelectList goes here....
    model.SearchCriteria = searchCriteriaSelectList;
    return View(model);
}
视图中的

@model SearchCriteriaViewModel

@{
    ViewBag.Title = "Index";
}

   <p>
    @Html.DropDownListFor(model => model.Id, 
           new SelectList(model.SearchCriteria , "Value", "Text"))
  </p>

答案 1 :(得分:2)

在过去3年中广泛使用ASP.NET MVC后,我更喜欢使用Html.EditorFor() method以上的 additionalViewData

[列表项] 作为anonymous对象,将相同的属性名称作为Model的属性传递到Html.EditorFor()方法。

好处是Html.EditorFor()方法会自动使用您的编辑器模板 因此,您无需为下拉列表提供CSS类名 见下面的比较。

//------------------------------
// additionalViewData                <=== RECOMMENDED APPROACH
//------------------------------
@Html.EditorFor(
    m => m.MyPropertyName,
    new { MyPropertyName = Model.ListItemsForMyPropertyName }
)

//------------------------------
//  traditional approach requires to pass your own HTML attributes
//------------------------------
@Html.DropDown(
    "MyPropertyName",
    Model.ListItemsForMyPropertyName,
    new Dictionary<string, object> {
        { "class", "myDropDownCssClass" }
    }
);

//------------------------------
//  DropDownListFor still requires you to pass in your own HTML attributes
//------------------------------
@Html.DropDownListFor(
    m => m.MyPropertyName,
    Model.ListItemsForMyPropertyName,
    new Dictionary<string, object> {
        { "class", "myDropDownCssClass" }
    }
);

如果您想了解更多详情,请参阅my answer in another thread here