question

yusuf2 avatar image
0 Votes"
yusuf2 asked ZhiLv-MSFT answered

Asp.net core - Field cant be empty

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

dotnet-csharpdotnet-aspnet-core-generaldotnet-aspnet-core-razor
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I can't leave the field empty in asp.net core razor pages

What field?

And does this have something to do with validating the model?

0 Votes 0 ·
yusuf2 avatar image yusuf2 karenpayneoregon ·

Thanks for the reply

I can't leave any field empty.
203698-image.png


0 Votes 0 ·
image.png (13.6 KiB)

You must be targeting .NET 6? Use a nullable type of the field is not required.


 public string? text { get; set; }
0 Votes 0 ·

1 Answer

ZhiLv-MSFT avatar image
0 Votes"
ZhiLv-MSFT answered

Hi @yusuf2,

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


image.png (29.6 KiB)
image.png (46.1 KiB)
2.gif (1.3 MiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.