Best way to handle multiple form validation in ASP.NET MVC -


overview

i have payment page allow user select payment method, either credit card or check. have form each of these payment methods. did not want create separate page each of these methods ui purposes, use [div] each form , toggle display jquery.

problem

each of payment methods has own set of validation rules. want able apply model validation payment method selected , submitted user. in order validation, required post same controller action, submitting different actions not option.

approaches

i have considered following approaches:

  1. partial validation based on incoming values, described here: http://blog.stevensanderson.com/2010/02/19/partial-validation-in-aspnet-mvc-2/. issue approach security implication, , relatively small app, don't have separate domain layer author of article suggests.
  2. create separate models each payment method, , overload controller [httppost] action different model parameter. tried compiler complains action ambiguous, though have this

    [httppost]
    public actionresult pay(creditcardpaymentmodel model) {...}

    [httppost]
    public actionresult pay(checkpaymentmodel model) {...}

  3. use separate controller actions handle individual form posts , use tempdata set validation messages , redirect form page display messages. don't option feels clunky, , don't using tempdata other simple messages.

i welcome suggestions on best way handle such situation cleanly , efficiently.

i go option 2, gives nice, clean separation between payment models , concrete model classes (i go typed solutions).

option 2 easy unit test...


Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -