2012-01-31 112 views
1

我需要在ASP.NET MVC 3填充下拉我希望得到的回答以下几个问题:填充下拉框在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

拉姆达=>的右侧有一个简单的类型不属于复杂型修改代码就像

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> 
+0

感谢您的代码。它可以稍作修改。但是,我无法理解DropDownListFor语法。模型=> model.Id部分代表什么?我们不会在任何地方填充ID。 – Yasir 2012-02-02 07:33:22

+0

它为指定表达式表示的对象中的每个属性返回一个HTML select元素,这里有更多关于它的详细信息http://msdn.microsoft.com/en-us/library/system.web.mvc。 html.selectextensions.dropdownlistfor.aspx – Rafay 2012-02-02 08:36:03

2

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

传递到自己的[清单项目]与作为示范的财产为Html.EditorFor()方法相同的属性名称anonymous对象。

好处是,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