Home > Net >  Checkbox default value
Checkbox default value

Time:10-20


    <div >
            <label asp-for="@Model.ProductDTO.Discontinued" >Discontinued</label>
            <input type="checkbox" asp-for="@Model.ProductDTO.Discontinued" name="Discontinued" />
        </div>

public bool? Discontinued { get; set; }


    InvalidOperationException: Unexpected 'asp-for' expression result type 'System.Nullable`1[[System.Boolean, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]]' for <input>. 'asp-for' must be of type 'System.Boolean' or 'System.String' that can be parsed as a 'System.Boolean' if 'type' is 'checkbox'.

The Discontinued model is NUllable so checkbox can be nullable. How can I do that?

CodePudding user response:

The asp-for attribute expects just the model property, not preceded by "Model":

<input type="checkbox" asp-for="ProductDTO.Discontinued" />

Also, you don't need to specify the name or id attribute when using asp-for. They are generated by the tag helper.

  • Related