我想重用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年多年?
可能重複http://stackoverflow.com/questions/5052752/adding-your-own-htmlhelper-in-asp-net-mvc-3) – 2014-10-12 06:26:11
如果你的意思是擴展方法沒有被加載,你應該爲擴展方法類存在的命名空間添加一個using語句 – mfarouk 2014-10-12 06:32:22
顯示輸出html – 2014-10-12 06:55:07