asp.net core validation is working incorrectly

S A 86 Reputation points
2022-03-16T07:27:12.873+00:00

asp.net core 3.1 - validation is not working correctly.

Developer technologies ASP.NET ASP.NET Core
{count} vote

6 answers

Sort by: Most helpful
  1. Sreeju Nair 12,666 Reputation points
    2022-03-16T07:36:54.037+00:00

    Thank you for posting your query in the Q & A. I believe, you need to provide some more information for the community members to give you solutions for your problem. However based on your post,, refer the following links.

    https://learn.microsoft.com/en-us/aspnet/core/mvc/models/validation?view=aspnetcore-6.0
    https://learn.microsoft.com/en-us/aspnet/core/mvc/models/validation?view=aspnetcore-6.0#client-side-validation
    https://learn.microsoft.com/en-us/aspnet/core/mvc/models/validation?view=aspnetcore-6.0#unobtrusive-validation

    Hope this helps


  2. AgaveJoe 30,126 Reputation points
    2022-03-16T11:14:52.667+00:00

    Model validation works exactly as documented. There must be mistakes in your code or design. Share enough code to reproduce what's "not working correctly" and the community will help fix the problem and/or explain the misunderstanding in your code.

    0 comments No comments

  3. S A 86 Reputation points
    2022-03-16T13:35:02.173+00:00

    The code is:
    CreateNew.cshtml:

    @Anonymous
    @默 Project.Pages.PersonalPage.CreateNew
    @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
    @{
    ViewData["Title"] = "Create new";
    Layout = "_Layout";
    }

    <div class="row">
    <div class="col-md-8">
    <form method="post" >
    <h4>Create New:</h4>
    <hr />
    <div asp-validation-summary="All" class="text-danger"></div>

               <div class="form-group">  
                    <label asp-for="Input.Category"></label>  
                    <select asp-for="Input.Category" class="form-control">  
                        <option value="" >=== Select ===</option>  
                        <option value="Value 1"> Value 1</option>  
                       <option value=" Value 2"> Value 2</option>      
                       <option value=" Value 3"> Value 3</option>     
                    </select>  
                    <span asp-validation-for="Input.Category" class="text-danger"></span>  
                </div>  
    
                 <div class="form-group">  
                    <label asp-for="Input.Title"></label>  
                    <input asp-for="Input.Title" class="form-control" />  
                    <span asp-validation-for="Input.Title" class="text-danger"></span>  
                </div>  
    
                <div class="form-group">  
                    <label asp-for="Input.Question"></label>  
                    <textarea id="editor" asp-for="Input.Question" class="form-control"></textarea>   
                    <span asp-validation-for="Input.Question" class="text-danger"></span>  
                </div>                 
                           
               <button type="submit" class="btn btn-primary">Create</button>                 
             
            </form>          
    </div>  
    

    </div>

    @section Scripts{
    <partial name="_ValidationScriptsPartial" />
    }

    ------------------------

    CreateNew.cshtml.cs:

    [AllowAnonymous]
    public class CreateNew : PageModel
    {

        [BindProperty]  
        public InputModel Input { get; set; }  
    
        public string ReturnUrl { get; set; }   
    
        public class InputModel  
        {  
            [Required]              
            public string Category { get; set; }  
    
            [Required]              
            public string Title { get; set; }  
    
            [Required]  
            [Display(Name = "Question")]  
            public string QuestionA { get; set; }        
             
        }  
    


  4. Rijwan Ansari 766 Reputation points MVP
    2022-03-16T16:24:37.287+00:00

    Hi @S A

    You can flow the below article step by step to add validation in razor pages.
    https://learn.microsoft.com/en-us/aspnet/core/tutorials/razor-pages/validation?view=aspnetcore-6.0&tabs=visual-studio


  5. AgaveJoe 30,126 Reputation points
    2022-03-16T19:40:04.36+00:00

    Hallo, thanks but it was my typing error. You are right, but the error is still the same...

    The community can only see the information you share in this forum. Again, what is the error? What are the steps to reproduce the error? Please be specific.

    Keep in mind, I created a sample using your code and the model validation works as expected. If the validation is not working as you expect then you must tell us your expectations. Don't make the community guess.

    Code

    using Microsoft.AspNetCore.Mvc;
    using Microsoft.AspNetCore.Mvc.RazorPages;
    using System.ComponentModel.DataAnnotations;
    
    namespace RazorPagesBasicDemo.Pages
    {
        public class CreateNew : PageModel
        {
            public void OnGet()
            {
            }
    
            public void OnPost()
            {
    
            }
    
            [BindProperty]
            public InputModel Input { get; set; }
            public string ReturnUrl { get; set; }
            public class InputModel
            {
                [Required]
                public string Category { get; set; }
                [Required]
                public string Title { get; set; }
                [Display(Name = "Question")]
                public string QuestionA { get; set; }
            }
        }
    }
    

    Markup

    @page
    @model RazorPagesBasicDemo.Pages.CreateNew
    @{
        ViewData["Title"] = "Create new";
        Layout = "_Layout";
    }
    
    <div class="row">
        <div class="col-md-8">
            <form method="post">
                <h4>Create New:</h4>
                <hr />
                <div asp-validation-summary="All" class="text-danger"></div>
    
                <div class="form-group">
                    <label asp-for="Input.Category"></label>
                    <select asp-for="Input.Category" class="form-control">
                        <option value="">=== Select ===</option>
                        <option value="Value 1"> Value 1</option>
                        <option value=" Value 2"> Value 2</option>
                        <option value=" Value 3"> Value 3</option>
                    </select>
                    <span asp-validation-for="Input.Category" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <label asp-for="Input.Title"></label>
                    <input asp-for="Input.Title" class="form-control" />
                    <span asp-validation-for="Input.Title" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <label asp-for="Input.QuestionA"></label>
                    <textarea id="editor" asp-for="Input.QuestionA" class="form-control"></textarea>
                    <span asp-validation-for="Input.QuestionA" class="text-danger"></span>
                </div>
    
                <button type="submit" class="btn btn-primary">Create</button>
    
            </form>
        </div>
    </div>
    
    
    
    @section Scripts{
    <partial name="_ValidationScriptsPartial" />
    }
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.