开发者

Enforcing different validation rules on a form with different submit buttons

I have a view with a form that is mapped to a ViewModel. I have 2 submit buttons in the form. When the form is submitted using 1 of the buttons, some of the fields in the form are going to be empty - is it possible to avoid validating the empt开发者_如何学运维y fields when the form is submitted using this 1 button? (Using the other button I would like to keep the usual validation rules).

Unfortunately I cannot seperate the fields that I expect to be empty into a seperate ViewModel because both models would have a common field that always needs to be populated.

Any ideas on this? Any help would be much appreciated.

Many thanks. James


It is possible, but

  • You will not be able to use clients-side validation
  • You will not be able to use data annotation attributes like Required etc and you probably need your own validation (or as option you may mark only properties are required in both cases and validate other properties manually)

You need to set different values for name attributes of your submits

<input type="submit" name="first-submit" value="First action" />
<input type="submit" name="second-submit" value="Second action" />

Then you may declare parameter of type FormCollection in your action method. Depends on which button was pressed it will contain first-submit key or second-submit key. Now you can alter your validation logic depends on this.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜