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>
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.
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.
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 .
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.
Is model state available in the view?
Of course:
@if (!ViewData.ModelState.IsValid) { <div>There are some errors</div> }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With