Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

IHtmlHelper<Model> does not contain a definition for <Foo>

I am migrating an application from ASP.NET 4.8 to ASP.NET Core 6.

I have the following in a ButtonExtensions class:

public static IHtmlContent HelpButton(this HtmlHelper htmlHelper, Enums.ButtonSize buttonSize = Enums.ButtonSize.Normal)
{
    var button = new TagBuilder("button");

    button.MergeAttribute("type", "button");
    button.MergeAttribute("aria-label", "Help Button");
    button.MergeAttribute("tabindex", "100");

    button.AddCssClass("btn");
    button.AddCssClass("btn-outline-info");
    button.AddCssClass("btn-light");
    button.AddCssClass("help-button");

    if (buttonSize == Enums.ButtonSize.Small)
        button.AddCssClass("btn-sm");
    else if (buttonSize == Enums.ButtonSize.Large)
        button.AddCssClass("btn-lg");

    return BuildButton("Help", BuildInternalIconTag("fa-question-circle"), button);
}

I try to use it in an Index.cshtml file as so:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

@Html.HelpButton()

I have the following in a FormLabelExtensions class:

public static IHtmlContent FormLabelFor<TModel, TValue>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TValue>> expression, object htmlAttributes = null)
{
    return FormLabelFor(htmlHelper, expression, null, htmlAttributes);
}

private static IHtmlContent FormLabelFor<TModel, TValue>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TValue>> expression, string labelText, object htmlAttributes)
{
    var attributes = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);

    if (attributes.ContainsKey("class"))
        attributes["class"] = $"{attributes["class"]} col-form-label";
    else
        attributes.Add("class", "col-form-label");

    var modelExpressionProvider = htmlHelper.ViewContext?.HttpContext?.RequestServices?.GetService<ModelExpressionProvider>()
        ?? new ModelExpressionProvider(htmlHelper.MetadataProvider);

    var modelExpression = modelExpressionProvider.CreateModelExpression(htmlHelper.ViewData, expression);

    if (!modelExpression.Metadata.IsRequired)
        return htmlHelper.LabelFor(expression, labelText, attributes);

    if (attributes.ContainsKey("class"))
        attributes["class"] = $"{attributes["class"]} required";
    else
        attributes.Add("class", "required");

    return htmlHelper.LabelFor(expression, labelText, attributes);
}

I try to use it in an Index.cshtml file as so:

@Html.FormLabelFor(m => model)

Both give me the same error.

>Solution :

Looks like you just need to change HtmlHelper<TModel> to use the interface version of that IHtmlHelper<TModel> as this is the proper way in .NET Core.

For example:

public static IHtmlContent HelpButton(this IHtmlHelper htmlHelper, Enums.ButtonSize buttonSize = Enums.ButtonSize.Normal)
{
    ...
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading