Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does it mean when a viewmodel contains [required]?

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?

like image 739
Samantha J T Star Avatar asked Dec 04 '25 07:12

Samantha J T Star


1 Answers

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 Required attribute, and a Html.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.