2014-10-12 110 views
0

我想重用HTML輔助發現hereMVC HTML輔助擴展

public static class HtmlHelperExtension 
{ 
    public static string DatePickerDropDowns(this HtmlHelper html, string dayName, string monthName, string yearName) 
    { 
     var daysList = new TagBuilder("select"); 
     var monthsList = new TagBuilder("select"); 
     var yearsList = new TagBuilder("select"); 

     daysList.Attributes.Add("name", dayName); 
     monthsList.Attributes.Add("name", monthName); 
     yearsList.Attributes.Add("name", yearName); 

     StringBuilder days = new StringBuilder(); 
     StringBuilder months = new StringBuilder(); 
     StringBuilder years = new StringBuilder(); 

     int beginYear = DateTime.UtcNow.Year - 100; 
     int endYear = DateTime.UtcNow.Year; 

     for (int i = 1; i <= 31; i++) 
      days.AppendFormat("<option value='{0}'>{0}</option>", i); 

     for (int i = 1; i <= 12; i++) 
      months.AppendFormat("<option value='{0}'>{0}</option>", i); 

     for (int i = beginYear; i <= endYear; i++) 
      years.AppendFormat("<option value='{0}'>{0}</option>", i); 

     daysList.InnerHtml = days.ToString(); 
     monthsList.InnerHtml = months.ToString(); 
     yearsList.InnerHtml = years.ToString(); 

     return string.Concat(daysList.ToString(), monthsList.ToString(), yearsList.ToString()); 
    } 
} 

但我怎麼使用我的MVC剃刀頁面上。我通過

@ Html.DatePickerDropDowns(「DAYNAME嘗試「,」monthName「,」yearName「)

但是當我加載我的剃刀頁面,它沒有工作,它只是顯示HTML語法,我的意思是它沒有顯示實際的下拉列表。 我在做什麼這裏錯了。我可以解決這個問題,並顯示三天下拉1天,1月和1年多年?

+0

可能重複http://stackoverflow.com/questions/5052752/adding-your-own-htmlhelper-in-asp-net-mvc-3) – 2014-10-12 06:26:11

+0

如果你的意思是擴展方法沒有被加載,你應該爲擴展方法類存在的命名空間添加一個using語句 – mfarouk 2014-10-12 06:32:22

+0

顯示輸出html – 2014-10-12 06:55:07

回答

1

您必須更改您的助手定義的回報。 確保您使用MvcHtmlString和返回return MvcHtmlString.Create(String.Concat(daysList.ToString(), monthsList.ToString(), yearsList.ToString()));

完整代碼:[ASP.NET中添加自己的HtmlHelper MVC 3](的

public static MvcHtmlString DatePickerDropDowns(this HtmlHelper html, string dayName, string monthName, string yearName) 
{ 
    var daysList = new TagBuilder("select"); 
    var monthsList = new TagBuilder("select"); 
    var yearsList = new TagBuilder("select"); 

    daysList.Attributes.Add("name", dayName); 
    monthsList.Attributes.Add("name", monthName); 
    yearsList.Attributes.Add("name", yearName); 

    StringBuilder days = new StringBuilder(); 
    StringBuilder months = new StringBuilder(); 
    StringBuilder years = new StringBuilder(); 

    int beginYear = DateTime.UtcNow.Year - 100; 
    int endYear = DateTime.UtcNow.Year; 

    for (int i = 1; i <= 31; i++) 
     days.AppendFormat("<option value='{0}'>{0}</option>", i); 

    for (int i = 1; i <= 12; i++) 
     months.AppendFormat("<option value='{0}'>{0}</option>", i); 

    for (int i = beginYear; i <= endYear; i++) 
     years.AppendFormat("<option value='{0}'>{0}</option>", i); 

    daysList.InnerHtml = days.ToString(); 
    monthsList.InnerHtml = months.ToString(); 
    yearsList.InnerHtml = years.ToString(); 

    return MvcHtmlString.Create(String.Concat(daysList.ToString(), monthsList.ToString(), yearsList.ToString())); 
} 
0

而不是返回string返回MvcHtmlString

public static MvcHtmlString DatePickerDropDowns(this HtmlHelper html, string dayName, string monthName, string yearName)