Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does ModelState.IsValid=true guarantee that the passed model parameter is not null?

Assume that we have Create and Edit action methods that are attributed by HttpPost and they have a model parameter of type, for example, BlogViewModel as follows.

[HttpPost]
public IActionResult Create(..., BlogViewModel model)
{
....
}

[HttpPost]
public IActionResult Edit(..., BlogViewModel model)
{
....
}

In their body, we usually do validation as follows.

if(ModelState.IsValid)
{
// do something 
}

Here, do something can be an operation accessing a property of the model.

Question

I am not sure whether or not there is a possibility in which model becomes null. If model is null then do something (such as accessing a property of model) will throw an exception.

I read many examples (from the internet and textbooks), I have not seen someone doing double check as follows yet.

if(model!=null)
{
    if(ModelState.IsValid)
    {
    // do something
    }
}

or

if(ModelState.IsValid)
{
    if(model!=null)
    {
    // do something
    }
}

Probably, the condition ModelState.IsValid is true guarantees that model is not null.

Is my assumption here correct? I am afraid I am doing a time-bomb assumption.

like image 483
xport Avatar asked Mar 20 '26 15:03

xport


2 Answers

To answer your question, no, ModelState.IsValid does not check if your model is null and will throw an error if that happens.

In an API it is quite easy to have null models, if you make a mistake while building your request model and it doesn't match what the endpoint expects.

Or someone else could look at your website, see the API calls and decides to have some fun and flood your API with requests which don't have valid models.

There are ways to check for null models in one place such as here : ModelState is valid with null model

like image 117
Andrei Dragotoniu Avatar answered Mar 22 '26 05:03

Andrei Dragotoniu


what is Model State ? The ModelState represents a collection of name and value pairs that were submitted to the server during a POST

What's in a ModelState? Here's what those values look like, from the same debugger session: enter image description here Each of the properties has an instance of ValueProviderResult that contains the actual values submitted to the server.

Validation errors in Model State... eg:-

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

[StringLength(50, ErrorMessage = "The Last Name must be less than {1} characters.")]
public string LastName { get; set; }

in this case if your LastName contain more than 50 caracters, ModelState if false, because vaueProvidedResult having exception

Hope you got the understating about ModelState

Answer for your question is you can handle model from modelState

if model null automatically modelState is not valid

like image 36
Shiwanka Chathuranga Avatar answered Mar 22 '26 05:03

Shiwanka Chathuranga



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!