locked
Model Validation Errors Displaying Prior to Submitting Form RRS feed

  • Question

  • User2029849071 posted

    Hello,

    I currently have jquery that gets called when the user selects a selection in a drop down control.  Based on the user selection the jquery calls a view component with view model passed and then displays the corresponding view in a div.  The issue I am having is that the model validation errors are being displayed on the view that is being passed by the view component when loaded and prior to the view being submitted.  I would like the validation errors only to display on submit if there is a validation error.  In addition, the validation does work as supposed to on submitting the view.

    I am still learning View Components so there is something I must be missing.  For some reason it is validating on load, which will fail because the fields are not populated.  I just want it to validate on submit.  Typically with my experience with just a view (no view components) using model validation the error doesn't doesn't display on load and only on submit.  

    Here is a link to a screen shot showing validation happening right after the view loads prior to submitting the view. 

    https://imgur.com/qZakdHs

    Any help is appreciated.

    Thanks,

    Tim

    Wednesday, October 16, 2019 10:33 PM

Answers

  • User711641945 posted

    Hi bigmac025,

    I came from this thread. From your code,your request forwards for two times on server side and in such case validation attribute would validate by default.

    For your scenerio,I suggest you could use client side validation like below:

    1.Model:

    public class WarrantyLabelsViewModel
    {
        public int SelectedClassID { get; set; }
    //Comment on the required attribute //[Required] public string PartNumber { get; set; } public string NumberOfCopies { get; set; } public LabelClass LabelClass { get; set; } }

    2.View:

    <input asp-for="PartNumber" type="text" class="form-control" required>
    <span asp-validation-for="PartNumber" class="text-danger" style="width: 200px; display:block;"></span>

    Best Regards,

    Rena

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 17, 2019 5:25 AM

All replies

  • User585649674 posted

    You are getting the validation messsages because, your jquery might be passing empty viewmodel, and view model class might be having Data annotation properties for required field validations.

    View component should be used for Viewing and updating already existing data in the database. 

    For creating data use a partial view. Create form should be a http get method with no input. An empty view model should be created and passed to View. And on post, model validation needs to happen.

    Jquery ui validation will trigger when there is a submit button in the form. The error message will be displayed

    Thursday, October 17, 2019 3:57 AM
  • User711641945 posted

    Hi bigmac025,

    I came from this thread. From your code,your request forwards for two times on server side and in such case validation attribute would validate by default.

    For your scenerio,I suggest you could use client side validation like below:

    1.Model:

    public class WarrantyLabelsViewModel
    {
        public int SelectedClassID { get; set; }
    //Comment on the required attribute //[Required] public string PartNumber { get; set; } public string NumberOfCopies { get; set; } public LabelClass LabelClass { get; set; } }

    2.View:

    <input asp-for="PartNumber" type="text" class="form-control" required>
    <span asp-validation-for="PartNumber" class="text-danger" style="width: 200px; display:block;"></span>

    Best Regards,

    Rena

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 17, 2019 5:25 AM