MVC html助手扩展

时间:2014-10-12 06:16:46

标签: c# asp.net-mvc

我正在尝试重用html helper found here

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(&#34; DAYNAME&#34;&#34; MONTHNAME&#34;&#34; yearName&#34)

但是当我加载我的剃刀页面时,它没有工作,它只是显示html语法,我的意思是它没有显示实际的下拉列表。 我在这里做错了什么。我可以解决这个问题并显示3个下拉列表1个月,1个月和1个月吗?

2 个答案:

答案 0 :(得分:1)

您必须更改助手的定义回报。 请务必使用MvcHtmlString和返回return MvcHtmlString.Create(String.Concat(daysList.ToString(), monthsList.ToString(), yearsList.ToString()));

完整代码:

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()));
}

答案 1 :(得分:0)

而不是返回string返回MvcHtmlString

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