I have a view model that looks like the following:
LandingViewModel.cs
public class LandingViewModel : IValidatableObject { [Required(ErrorMessage = "Required")] [Display(Name = "State")] public string RatingState; IEnumerable<ValidationResult> IValidatableObject.Validate(ValidationContext validationContext) { // just testing to see if POST will validate yield return new ValidationResult("Required", new[] { "RatingState" }); } }
I have a view that looks like the following:
Landing.cshtml
@model Project.Features.Landing.LandingViewModel<!-- removed code --><label asp-for="RatingState" class="description-label col-sm-2"></label>
I have the following in my controller:
LandingController.cs
[HttpGet] [Route("/landing")] public IActionResult Landing() { var viewModel = new LandingViewModel(); return View(viewModel); }
When my Landing view renders I'm not seeing the 'State' label...I see 'RatingState'. Also, if I POST, the [Required] attribute isn't setting the ModelState to invalid. However, my IValidatableObject does work and will validate the view correctly.
Why aren't my attributes working? What am I missing?