Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

Model Attributes Not Working

$
0
0

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?


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>