My project has viewmodels labeled like this:
public class locViewModel {
[Required]
public string City { get; set; }
}
If the view does not set a value then how can I detect this? Is this how the [required] works? Also what other kind of tags can I add to fields in a viewModel?
That means that for validation purposes you can do numerous things. For instance, in a View you can have client validation enabled and the form will not submit unless the control that is populating that property has data entered into it.
With a property with the
Requiredattribute, and aHtml.ValidationMessageFor(m => m.City, "City is required")you can notify the user on the client-side that it is a required field.Here is a Great Resource on unobtrusive validation, and an in depth explanation of what you are looking for.