locked
Validation Message Not Displaying RRS feed

  • Question

  • User2029849071 posted

    Hello,

    I am having difficulties getting a validation message for a drop down box displaying from model validation.  The view will not submit if a selection is not made (so I know model validation is working), but the validation message will not display.  I am using a remote descriptor in the model for validation and I can confirm it is being called in the controller:

    The asp-validation-for helper tag in question (see bold) that will not display:

    <label class="control-label">Printer</label>
    <select asp-for="SelectedPrinterID" asp-items="@Model.LabelPrinters" class="form-control" style="height: 60px;">
        <option selected>Select a Printer</option>
    </select>
    <span asp-validation-for="SelectedPrinterID" class="text-danger" style="width: 500px; display: block;"></span>

    The view the asp-validation-for helper tag in question is located in:

    @model OnTarget.Models.Warranty.WarrantyLabelsViewModel
    
    <p style="color:black">Warranty Standard Case Label (400)</p>
    
    <label class="control-label">Part Number</label>
    
    <label asp-for="NumberOfCopies" class="control-label"></label>
    <input asp-for="NumberOfCopies" type="text" value="1" style="width: 100px" ; class="form-control">
    <span asp-validation-for="NumberOfCopies" class="text-danger" style="width: 500px; display:block;"></span>
    
    <br />
    
    <label class="control-label">Printer</label>
    <select asp-for="SelectedPrinterID" asp-items="@Model.LabelPrinters" class="form-control" style="height: 60px;">
        <option selected>Select a Printer</option>
    </select>
    <span asp-validation-for="SelectedPrinterID" class="text-danger" style="width: 500px; display: block;"></span>
    
    <br />
    <br />
    
    <div class="form-group">
        <input type="submit" value="Print Label(s)" class="btn btn-primary" />
    </div>
    
    
    @section Scripts {
        @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
    
    }
    
    <script src="~/lib/jquery/dist/jquery.js"></script>
    <script src="~/lib/jquery-validation/dist/jquery.validate.js"></script>
    <script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.js"></script>
    

    Model:

    using Microsoft.AspNetCore.Mvc;
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.Threading.Tasks;
    using Microsoft.AspNetCore.Mvc.Rendering;
    
    namespace OnTarget.Models.Warranty
    {
        public class WarrantyLabelsViewModel
        {
          
            [Remote(action: "LabelClassSelected", controller: "Warranty", ErrorMessage = "Please Select a Label Class")]
            [Display(Name = "Label Class")]
            public int SelectedClassID { get; set; }
    
            [Display(Name = "Select a Label Class")]
            public List<SelectListItem> LabelClasses { get; set; }
    
            //[Required(AllowEmptyStrings = false, ErrorMessage = "Please Enter a Part Number")]
            [Display(Name = "Part Number")]
            public string PartNumber { get; set; }
    
            [Range(0, int.MaxValue, ErrorMessage = "Please enter valid Number")]
            [Required(ErrorMessage = "Please Enter Number of Copies")]
            [Display(Name = "Number of Copies")]
            public int NumberOfCopies { get; set; }
    
            [Remote(action: "PrinterSelected", controller: "Warranty", ErrorMessage = "Please Select a Printer")]
            [Required(ErrorMessage = "Please Select a Printer")]
            [Display(Name = "Printer")]
            public int SelectedPrinterID { get; set; }
    
            [Display(Name = "Select a Printer")]
            public List<SelectListItem> LabelPrinters { get; set; }
        }
    }
    

    Controller:

    [AcceptVerbs("Get", "Post")]
            public IActionResult PrinterSelected(int selectedPrinterID)
            {
                System.Diagnostics.Debug.WriteLine("Selected Printer ID " + selectedPrinterID);
    
                bool result;
    
                if (selectedPrinterID == 0)
                {
                    result = false;
                }
                else
                {
                    result = true;
                }
    
                System.Diagnostics.Debug.WriteLine(result);
    
                return Json(result);
            }

    Any help would be appreciated.

    Thanks,

    Tim

    Friday, November 1, 2019 7:56 AM

Answers

  • User475983607 posted

    IMO, the approach does not make sense.  Why do an HTTP request to check if the selected value is 0?  You already know if the value is zero there is no logical reason to send the value to an action.   Secondly,  if you are clicking a submit button and refreshing the page then the error message is probably over written.  

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 1, 2019 11:05 AM