2011-12-27 59 views
1

我想創建一個自定義控件(局部視圖,模板),並使其功能類似於html.dropdownlistfor(),它需要一個集合從中進行選擇,另一個字段用於保存選定的值。爲了做到這一點,我想查看dropdownlistfor的代碼。由於ASP.NET MVC是開源的,我在哪裏可以找到代碼/剃鬚刀標記dropdownlistfor?我在哪裏可以找到dropdownlist的默認剃鬚刀編輯器模板?

+0

http://aspnet.codeplex.com/SourceControl/changeset/view/72551#338505 – 2011-12-27 22:21:01

+0

我沒有在那裏看到DropDownListFor。我想弄清楚DropDownListFor如何與DropDownList相關。 – 2011-12-27 22:30:38

回答

1

它在MVC源代碼中的SelectExtensions

SelectExtensions.cs

如果你問的源代碼,你可以得到在CodePlex上,here

+0

我沒有在那裏看到DropDownListFor。我想弄清楚DropDownListFor如何與DropDownList相關。 – 2011-12-27 22:30:29

3

DropdownListFor不使用的模板。這只是一個帶選項的選擇標籤。 DropDownListFor和DropDownList是相同的,除了DropDownListFor使用強類型的lambda表達式。他們都生成相同的HTML。

0

爲什麼你需要「另一個字段來保存選定的值。」?

List<SelectListItem> items = new List<SelectListItem>(); 
     items.Add(new SelectListItem { Text = "Action", Value = "0" }); 
     items.Add(new SelectListItem { Text = "Drama", Value = "1" }); 
     items.Add(new SelectListItem { Text = "Comedy", Value = "2", Selected = true }); 
     items.Add(new SelectListItem { Text = "Science Fiction", Value = "3" }); 

正如你所看到的,SelectListItem有一個Selected字段。我有一個待發的DDL教程可以發給你。它有一個示例,您可以在DDL旁邊使用文本輸入來爲DDL添加新類別。這是在jQuery中實現的。代碼是在Using the DropDownList and ListBox with ASP.NET MVC射擊我一封電子郵件,我會給你發送教程。 ricka.anderson [at] microsoft.com

相關問題