Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I check ModelState.IsValid from inside my Razor view [duplicate]

I have the following in my action method:

       if (!ModelState.IsValid)         return View(vm); 

In the view I want to not present a submit key to allow deletion if the model state is not valid. Is there a way that I can do this? Is model state available in the view?

Update: I have implemented this based on the answers I was given:

            <div class="adm_td0" style=" padding: 0;">               @if (ViewData.ModelState.IsValid) {                 <input type='submit' value='Delete' name='SubmitAction' />             }                 <input type='submit' value='Cancel' name='SubmitAction' />             </div> 
like image 658
Samantha J T Star Avatar asked Dec 14 '11 15:12

Samantha J T Star


People also ask

How do I know if my ModelState IsValid?

Just place them just in front / at the place where you check ModelState. isValid and hover over the ModelState. Now you can easily browse through all the values inside and see what error causes the isvalid return false.

How do I find ModelState errors in view?

To pass error to the view we can use ModelState. AddModelError method (if the error is Model field specific) or simply ViewBag or ViewData can also be used.

Which ModelState is IsValid?

ModelState. IsValid indicates if it was possible to bind the incoming values from the request to the model correctly and whether any explicitly specified validation rules were broken during the model binding process. In your example, the model that is being bound is of class type Encaissement .

What does it mean if ModelState IsValid is false?

IsValid is false now. That's because an error exists; ModelState. IsValid is false if any of the properties submitted have any error messages attached to them. What all of this means is that by setting up the validation in this manner, we allow MVC to just work the way it was designed.


1 Answers

Is model state available in the view?

Of course:

@if (!ViewData.ModelState.IsValid) {     <div>There are some errors</div> } 
like image 93
Darin Dimitrov Avatar answered Nov 26 '22 20:11

Darin Dimitrov