jmsandiegoo
jmsandiegoo

Reputation: 463

difference between model object and model state in MVC

So I am wondering if someone could briefly explain the difference between a model object and a model state thank you!

Upvotes: 2

Views: 847

Answers (1)

Bon Macalindong
Bon Macalindong

Reputation: 1320

Model is simply just a class containing properties that represents certain object in your application. In MVC you can decorate your properties with DataAnnotations which can be used to validate your model.

e.g.

public class Person
{
    [Required]
    public string FirstName { get; set; }

    [Required]
    public string LastName { get; set; }
}

ModelState, as the name implies is the state of your Model as if it's valid. The MVC pipeline validates your model using the DataAnnotations that you've placed in your model properties. That's why you'll encounter a lot of if(ModelState.IsValid) calls in your controller to make sure that data submitted to the controller is valid.

Upvotes: 2

Related Questions