Search code examples
asp.net-corerazorasp.net-core-mvcscaffolding

Scaffolding the view in vs2022 is generating asp syntax not razor syntax


I am using .net core and now when I use the add view, it scaffolds the view in asp syntax and not razor syntax like it used to do. I picked add a view -> edit and now it is creating something like this:

<form asp-action="Index">
    <div asp-validation-summary="ModelOnly" class="text-danger"></div>
    <div class="form-group">
        <label asp-for="TracisNum" class="control-label"></label>
        <input asp-for="TracisNum" class="form-control" />
        <span asp-validation-for="TracisNum" class="text-danger"></span>
    </div>

But it used to create it with @html.beginform and @html.displayfor and @html.textfor is this something new in 2022?


Solution

  • I am using .net core and now when I use the add view, it scaffolds the view in asp syntax and not razor syntax like it used to do

    Well, based on your scenario and description, yes right you are. The @html.beginform is a Razor syntax that generates an HTML form element and corresponding hidden input elements for anti-forgery tokens. It is available in ASP.NET MVC versions up to 5, but not in ASP.NET Core MVC versions 6 and above.

    The reason why @html.beginform is not available in .NET core or latest version is because ASP.NET Core MVC has introduced a new way of working with forms using Tag Helpers.

    Tag Helpers are server-side code that helps to generate and modify HTML elements. They are more expressive, readable, and maintainable than the Razor syntax.

    For example, instead of using @html.beginform, you can use the <form> Tag Helper with attributes like asp-controller, asp-action, and asp-route to specify the form’s action, controller, and route values. Let's have a look following example:

    <form asp-controller="Demo" asp-action="Register" method="post">
        <div class="form-group">
            <label asp-for="Name"></label>
            <input asp-for="Name" class="form-control" />
        </div>
        <div class="form-group">
            <label asp-for="Email"></label>
            <input asp-for="Email" class="form-control" />
        </div>
        <button type="submit" class="btn btn-primary">Register</button>
    </form>
    

    Using tag helper like from or Input tag helper like asp-for you could achieve the same implementation to bind the input elements to the model properties.

    But it used to create it with @html.beginform and @html.displayfor and @html.textfor is this something new in 2022?

    As said earlier, @html.beginform and @html.displayfor are used to available upto asp.net MVC up to 5 version.

    The reason why the tag has been changed in ASP.NET Core 8 is because of the introduction of Tag Helpers, which are a new feature that can generate and modify HTML elements in Razor views. Tag Helpers are similar to HTML Helpers, but they use a more natural syntax that resembles HTML.

    Tag Helpers are not meant to replace HTML Helpers completely, but rather to offer an alternative way of creating HTML that is more readable, expressive, and intuitive. HTML Helpers are still available and useful for some scenarios, such as displaying or editing model properties that do not have a corresponding HTML element, or creating custom helpers that are not based on HTML tags.

    Therefore, if you still wants can used them but while you Scaffold it would used latest tag helpers.

    In addition, one cool thing in newer version of Tag Helper is you can create your own tag, you can customize them.

    Note: Please refer to this official document for more insight.