0

I am now working on an existing asp.net MVC application which has a few submit button in one web page(e.g. ignore, delete, update, submit...) And they all send a post request to the server and the server will perform different operation based on the request action type (e.g. ignore, delete, update, submit).

However, an issue is raised that the model validation will always triggered regardless of the operation. According to the business rule, we only need to perform validation for some certain operations. I can clear the error in the modelstate after the validation. (for example MVC3 Remove ModelState Errors).

However I am hoping there is a better way to just bypass the model validation according to the operation parameter from the client.

BTW: I am not allowed to significantly change the way how it works (e.g. send to another action for those operation that does not care about the validation), so I am hoping there is some trick that I can use to achieve this easily.

Community
  • 1
  • 1
Stay Foolish
  • 3,636
  • 3
  • 26
  • 30

1 Answers1

0

I assume now you are checking model state errors like...

if (ModelState.Valid)
{... valid and do something...}

If so, you can include some additional checking before hand that will be considered in your conditional. For instance, if it is a delete submit, you can check that before hand.

bool isDelete = ***check some value***

if (isDelete || ModelState.Valid)
{... valid and do something...}

This way even if the model state is invalid, it will still move forward.

One thing to consider is you will need to set something in the model to tell you what action is happening. This probably means using javascript to capture the submit, set a model field, and then perform the submit.

Trey Gourley
  • 391
  • 1
  • 7
  • 19
  • I might add that it is probably best practice to have separate actions for each of the action types you mentioned. That way you don't have to go through the contortions of figuring it out and they can each be controlled by their own model validation. – Trey Gourley Jan 09 '14 at 15:13