HiddenInput not working on ASP.NET Core 2.2 Scaffolding

asp.net-core asp.net-mvc-scaffolding entity-framework-core razor

Question

When using scaffolding in asp.net core 2.2, is there a way to make properties scaffold as Hidden Inputs?

Unfortunately [HiddenInput(DisplayValue=false)] does not seem to work...

1
0
12/16/2018 12:21:17 AM

Accepted Answer

HiddenInput(DisplayValue=false) is Ok and causes the property scaffold as hidden.

Note that after scaffolding, it doesn't generate an input of type Hidden in your code, instead when you run the application <input asp-for="HiddenProperty" /> becomes <input type="hidden" />.

Here is a sample:

public class Person
{
    public int Id { get; set; }

    public string Name { get; set; }

    [HiddenInput(DisplayValue = false)]
    public int? Age { get; set; }
}

View:

<form asp-action="Create">
        <div asp-validation-summary="ModelOnly" class="text-danger"></div>

        <div class="form-group">
            <label asp-for="Name" class="control-label"></label>
            <input asp-for="Name" class="form-control" />
            <span asp-validation-for="Name" class="text-danger"></span>
        </div>

        <div class="form-group">
            <label asp-for="Age" class="control-label"></label>
            <input asp-for="Age" class="form-control" />
            <span asp-validation-for="Age" class="text-danger"></span>
        </div>

        <div class="form-group">
            <input type="submit" value="Create" class="btn btn-primary" />
        </div>
    </form>

Result: Hidden input

2
12/15/2018 7:30:21 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow