HtmlExtensions looking for this HtmlHelper<TModel> htmlHelper

1.5k Views Asked by At

i've attempted to add some htmlextensions to my mvc project. When i try to use them they are all expecting a this HtmlHelper htmlHelper parameter? but according to all examples these are not expected.. what i am doing wrong?

public static string RadioButtonListFor(this HtmlHelper htmlHelper, Expression> expression, String tagBase) where TModel : class { return htmlHelper.RadioButtonListFor(expression, 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();
    }
2

There are 2 best solutions below

2
On

You are writing extension methods for HtmlHelper class. When ever you want to use your extension method you have to import the namespace in which your extension method is in.

Say for example RadioButtonListFor is in MyNamespace

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

Now in your view you have to import MyNamespace to use this extension method. You can import a namespace in Razor by specifying it like this on the top of the page.

@using MyNamespace
0
On

I wrote up a post that covers creating extension methods for the HtmlHelper.DropDownList helper. Check it out...it might be of help. I cover the DropDownList and DropDownListFor methods and go over including the reference to the namespace for your extension method class in both the Razor view file and the web.config.

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