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:
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);
}
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.
Member
11 Points
14 Posts
Validation Message Not Displaying
Nov 01, 2019 07:56 AM|bigmac025|LINK
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:
The view the asp-validation-for helper tag in question is located in:
Model:
Controller:
Any help would be appreciated.
Thanks,
Tim
All-Star
53081 Points
23655 Posts
Re: Validation Message Not Displaying
Nov 01, 2019 11:05 AM|mgebhard|LINK
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.