Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Partial validation of ASP.NET MVC 3 Models

I have a very large 60+ question form that the user can start to fill, save at any point and leave it in hold. The form can be reloaded from the database and completed any time and then close it.

I have the following model:

public class Questionnaire{

  [Required]
  public string Question1 { get; set; }

  [Required]
  [Range(1, 10)]
  public int Quesiton2 { get; set; }

  public string Question3 {get;set}
}

I need to partially validate my model when the user decides to save the form and perform a full validation including the validation of the required fileds when the user chose to close the form.

what is the best way to implement it ?

like image 487
Marc Avatar asked Mar 06 '11 14:03

Marc


1 Answers

Its not totally clear from your question / example exactly what you need, but I have found generally that splitting up my View Models in MVC is the best way to approach this kind of thing.

i.e. split up your entity into parts, each of which can be validated on its own.

Then use partial views / custom editor templates to provide the UI components for each of these.

Then you can either combine these components in one form when needed, or provide separate forms on same page, multi page wizards, or single page progressive AJAX wizards, or whatever you want) as needed.

Keeps things DRY and simple.

Key is to not be afraid to add the extra layer of View Models when needed, to bring your data objects in line with the requirements of your UI.

Don't be constrained by your business objects / entities when you specifically need to be free of them - its easy enough to put the parts of a business object back together from constituent View Model pieces.

like image 144
MemeDeveloper Avatar answered Nov 09 '22 23:11

MemeDeveloper