【问题标题】:HtmlExtensions looking for this HtmlHelper<TModel> htmlHelperHtmlExtensions 正在寻找这个 HtmlHelper<TModel> htmlHelper
【发布时间】:2011-06-23 21:14:23
【问题描述】:

我尝试将一些 htmlextensions 添加到我的 mvc 项目中。当我尝试使用它们时,他们都期待这个 HtmlHelper htmlHelper 参数?但根据所有示例,这些都不是预期的......我做错了什么?

public static string RadioButtonListFor(this HtmlHelper htmlHelper, Expression> expression, String tagBase) where TModel : class { 返回 htmlHelper.RadioButtonListFor(表达式, tagBase, null); }

    public static string RadioButtonListFor<TModel>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, RadioButtonListViewModel>> expression, String tagBase, object htmlAttributes) where TModel : class
    {
        return htmlHelper.RadioButtonListFor(expression, tagBase, new RouteValueDictionary(htmlAttributes));
    }

    public static string RadioButtonListFor<TModel>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, RadioButtonListViewModel>> expression, String tagBase, IDictionary<string, object> htmlAttributes) where TModel : class
    {
        var inputName = tagBase;
        RadioButtonListViewModel radioButtonList = GetValue(htmlHelper, expression);

        if (radioButtonList == null)
            return String.Empty;

        if (radioButtonList.ListItems == null)
            return String.Empty;


        var containerTag = new TagBuilder("td");
        containerTag.MergeAttribute("id", inputName + "_Container");
        foreach (var item in radioButtonList.ListItems)
        {
            var radioButtonTag = RadioButton(htmlHelper, inputName, new SelectListItem { Text = item.Text, Selected = item.Selected, Value = item.Value.ToString() }, htmlAttributes);

            containerTag.InnerHtml += radioButtonTag;
        }

        return containerTag.ToString();
    }

【问题讨论】:

    标签: asp.net-mvc-3 htmlextensions


    【解决方案1】:

    您正在为HtmlHelper 类编写扩展方法。当你想使用你的扩展方法时,你必须导入扩展方法所在的命名空间。

    比如说RadioButtonListForMyNamespace

    namespace MyNamespace
    {
        public static class HtmlExtensions
        {
            public static string RadioButtonListFor<TModel>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, RadioButtonListViewModel>> expression, String tagBase, object htmlAttributes) where TModel : class
            {
                 return htmlHelper.RadioButtonListFor(expression, tagBase, new RouteValueDictionary(htmlAttributes));
            }
        }
    }
    

    现在在您看来,您必须导入 MyNamespace 才能使用此扩展方法。 您可以在 Razor 中通过在页面顶部这样指定命名空间来导入命名空间。

    @using MyNamespace
    

    【讨论】:

    • 我已经完成了这个......并且我可以访问方法/扩展。只是在我看来,当我尝试使用它时,它会询问我第一个参数,但它不应该。我认为它要求我传递一个自身的实例?
    • @billy 你可以发布视图吗?
    【解决方案2】:

    我写了一篇文章,介绍了为 HtmlHelper.DropDownList 助手创建扩展方法。看看吧。。可能会有帮助。我介绍了DropDownListDropDownListFor 方法,并在Razor 视图文件和web.config 中包含了对扩展方法类的命名空间的引用。

    Populate html select lists from data in a view model in an ASP.NET MVC 3 application

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-28
      • 1970-01-01
      • 1970-01-01
      • 2023-03-28
      相关资源
      最近更新 更多