开发者

MVC Model Validation Across Multiple Pages

For a current MVC3 project I have a model that has multiple pages for input. The object is开发者_如何学Go set up with required fields however they're on different pages. This means I can't do a simple check on ModelState.IsValid. Instead I check if the current page has all the values it needs before allowing the user on to the next. This doesn't seem ideal.

One of the main things that I don't like is the fact that having required fields on a page other than the first means the postback and overall validation failure has already happened so any required fields on the next page are already marked in error state even though the user has never been to the page.

Are there any best practices for validation of an entity with multiple input pages that have required fields on them?


You should use view models instead of your domain models. Soy you could split your domain model into multiple view models for each step of the wizard. Here's another answer you might find useful.


This is one reason why using a separate ViewModel for every page is recommended as a best practice, and using your Entity classes directly in the ViewModel is discouraged.

With separate ViewModels, you can tailor the attribute-based validation as needed. It will complicate things in that you will then need to find a way to map between the ViewModels and your Entities.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜