开发者

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 followin开发者_运维百科g 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 ?


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.


Why are you using Question1.. Questionn.. 1 object per question, what if you add a new question?... you should have a Question List, and then, validate manually before save acording to your business rules.


You can have two seperate Actions Save() and Submit() and disable validation (or do minimal validation) on Save().


Following on from Bala R's idea, it would be best to perform conditional validation based on which action you are performing, saving or submitting. By this I mean, on save, only validate the data in the fields, so check for nulls/empties/defaults and don't validate those fields, but do validate the fields according to business logic/model rules for those which have been entered.

So, say you have a form with name, age and sex, on attempt 1, the user fills in their name as '1234', age as '12' and leaves 'sex' blank, when you save, you need to loop through each control and validate those that have values, in this case, name and age, then you can validate name and report that '1234' is not a valid name, but accept age as 12.

You would probably be better placed do this server side as if you tried to validate this client side, it would be hard to differentiate between a required field which has been left blank, with the intention of filling in later, and one which had been left blank full stop. (You could argue that you'd need a client side Save() and Submit() validation function to get round this as well as server side).

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜