2012-01-31 225 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