Asp.net core - Field cant be empty

Yusuf 691 Reputation points
2022-05-19T12:58:02.46+00:00

Hello
I can't leave the field empty in asp.net core razor pages
I created KRUD Pages using this model

public class NewsModel  
    {  
        [Key]  
        public int id { get; set; }  
        public string title { get; set; }  
        public string text { get; set; }  
        public string image { get; set; }  
        public bool displayInCarousel { get; set; }  
          
  
    }  

Then I used this command:

[Folder] > Add > New Scaffolded Item > Razor Pages using Entity Framework (KRUD)

@page  
@model MySite.Pages.Admin.News.CreateModel  
  
@{  
    ViewData["Title"] = "Create";  
}  
  
<h1>Create</h1>  
  
<h4>NewsModel</h4>  
<hr />  
<div class="row">  
    <div class="col-md-4">  
        <form method="post">  
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>  
            <div class="form-group">  
                <label asp-for="NewsModel.title" class="control-label"></label>  
                <input asp-for="NewsModel.title" class="form-control" />  
                <span asp-validation-for="NewsModel.title" class="text-danger"></span>  
            </div>  
            <div class="form-group">  
                <label asp-for="NewsModel.text" class="control-label"></label>  
                <input asp-for="NewsModel.text" class="form-control" />  
                <span asp-validation-for="NewsModel.text" class="text-danger"></span>  
            </div>  
            <div class="form-group">  
                <label asp-for="NewsModel.image" class="control-label"></label>  
                <input asp-for="NewsModel.image" class="form-control" />  
                <span asp-validation-for="NewsModel.image" class="text-danger"></span>  
            </div>  
            <div class="form-group form-check">  
                <label class="form-check-label">  
                    <input class="form-check-input" asp-for="NewsModel.displayInCarousel" /> @Html.DisplayNameFor(model => model.NewsModel.displayInCarousel)  
                </label>  
            </div>  
            <div class="form-group">  
                <input type="submit" value="Create" class="btn btn-primary" />  
            </div>  
        </form>  
    </div>  
</div>  
  
<div>  
    <a asp-page="Index">Back to List</a>  
</div>  
  
@section Scripts {  
    @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}  
}  

Thank you in advance

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,207 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,306 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Zhi Lv - MSFT 32,021 Reputation points Microsoft Vendor
    2022-05-20T03:07:35.527+00:00

    Hi @Yusuf ,

    C# 8.0 introduces nullable reference types, which complement reference types the same way nullable value types complement value types. You declare a variable to be a nullable reference type by appending a ? to the type. For example, string? represents a nullable string. You can use these new types to more clearly express your design intent: some variables must always have a value, others may be missing a value.

    Prior to .NET 6, new projects do not include the Nullable element. Beginning with .NET 6, new projects include the <Nullable>enable</Nullable> element in the project file. Like this:

    203855-image.png

         public string title { get; set; }  
         public string text { get; set; }  
         public string image { get; set; }  
         public bool displayInCarousel { get; set; }  
    

    So, in your code, if you enable the Nullable in the project file and use the above code, it means these properties are non-nullable property. So, when submit the form, it will show the property is required validation error.

    To solve this issue, you can try to use the following solutions:

    1. Make the properties nullable, add the ? like this:
       public string? title { get; set; }  
       public string? text { get; set; }  
       public string? image { get; set; }  
      
    2. Disable the Nullable feature: open the project file and change the enable to disable, like this:

    203867-image.png

    In this scenario, there is no need to add '?' for the property. If you want to make the property required, you need to add the [Required] attribute. The result like this:

    203847-2.gif


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,
    Dillion

    0 comments No comments