Search code examples
c#asp.net-corerazorrazor-pages

HtmlHelpers Html.LabelFor/TextBoxFor/EditorFor are unrecognized in ASP.NET Core Razor Pages


I have a .NET 5.0 MVC project migrated from .NET Core 3.1, but forms using Html.LabelFor, Html.TextBoxFor, or Html.EditorFor do not show inputs.

View:

@model UpdateProductsViewModel;

@{
    using (Html.BeginForm("UpdateProducts", "TheController", FormMethod.Post))
    {
        <p>Browse File:</p>
        Html.LabelFor(m => m.FullName, htmlAttributes: new { @class = "this-doesnt-show"});
        Html.TextBoxFor(m => m.FullName, htmlAttributes: new { @class = "this-neither");
        Html.EditorFor(m => m.FullName, htmlAttributes: new { @class = "this-neither");
        <div>
            <label asp-for="FullName" class="this-shows-with-asp-for"></label>
            <input asp-for="FullName" class="this-shows-with-asp-for" />
        </div>
        <input type='submit' value="Submit" />
    }
}

Model:

public class UpdateProductsViewModel{
  // truncated

  public string FullName { get; set; }
}

In my sample above, I have the model and view.

However before I changed it to asp-for, I had them as Html.LabelFor, Html.EditorFor, Html.TextboxFor. These have never shown again in .NET 5.

When I use the syntax <input asp-for=""> | <label asp-for=""> the controls show, in other words 1x label, 1x input text box, but none of the others (HtmlHelper syntax).

  • Are these Helpers not available anymore, even though IntelliSense detects the keywords in VS Code, or am I missing some references or some attributes on the ViewModel?

Please note I do not use .NET MVC with Razor that regularly, so maybe something changed that I am not aware of.


Solution

  • You need to use razor syntax. Something like this

    @model UpdateProductsViewModel;
    
    @{
        using (Html.BeginForm("UpdateProducts", "TheController", FormMethod.Post))
        {
            <p>Browse File:</p>
             @Html.LabelFor(m => m.FullName, htmlAttributes: new { @class = "this-doesnt- 
             show"})
             @Html.TextBoxFor(m => m.FullName, htmlAttributes: new { @class = 
             "thisneither")
              @Html.EditorFor(m => m.FullName, htmlAttributes: new { @class = 
              "thisneither")
            <input type='submit' value="Submit" />
        }
    }