0

I have created a custom html helper for an mvc5 application I am working on to create a dropdownlist for countries

  public static MvcHtmlString CountryDropDownListFor<TModel, TEnum>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TEnum>> expression, string css, bool displayPleaseSelect = true)
    {
        var countries = GetCountries();
        var list = countries.Select(c => new SelectListItem()
        {
            Selected = false,
            Text = c.Name,
            Value = c.Id
        }).ToList();
        if (displayPleaseSelect)
        {
            list.Insert(0, new SelectListItem() { Selected = true, Text = "- Please Select -", Value = "0" });
        }

        return htmlHelper.DropDownListFor(expression, list, new { @class = css });
    }

I think my helper looks ok however when adding it to an editor template view like

@Html.CountryDropDownListFor(m=>m.CountryId,"")

I get an error

'System.Web.Mvc.HtmlHelper' does not contain a definition for 'CountryDropDownListFor' and no extension method 'CountryDropDownListFor' accepting a first argument of type 'System.Web.Mvc.HtmlHelper' could be found (are you missing a using directive or an assembly reference?)

Can anyone see what I am doing wrong? The helper is in the same project

Diver Dan
  • 9,953
  • 22
  • 95
  • 166
  • Have you imported the namespace in your view of the file containing your function? (`@using ...`) – Marthijn Dec 17 '13 at 10:12
  • Within the template if I type @Html. I get access to CountryDownListFor via so I think its ok – Diver Dan Dec 17 '13 at 10:21
  • Does the extension method lives in the same assembly? If not, are you sure it's built properly? – Henk Mollema Dec 17 '13 at 10:26
  • Sorry @Marthijn was correct. I think resharper was playing tricks. I added reference in the editortemplate and its ok now. Thank you – Diver Dan Dec 17 '13 at 10:28
  • Glad you fixed it. If you do not want to use `@using` in each view, take a look at: http://stackoverflow.com/questions/3875207/how-to-add-extra-namespaces-to-razor-pages-instead-of-using-declaration – Marthijn Dec 17 '13 at 10:40

0 Answers0