0

I am currently working on a project in MVC 3 where I am leveraging Entity Framework to persist one data model over two Views which each contain one HTML Form (similar to wizard-based design).

Yet after the user fills out the appropriate fields on the first View and submits the form, client-side validation for the entire model is triggered, and validation errors are shown for fields that will not even be available for input until the second View instantiates.

I have currently implemented a workaround where I simply turn off client-side validation for the first View entirely, but I am certainly not keen on the idea of populating my model with data that has not been validated at all. This is bad. M'kay.

Is there any way to partially validate the fields on the first View and not trigger valdiation for the whole data model?

user1449244
  • 53
  • 1
  • 5
  • 1
    can you show us the code for the first view please and tell us which ones are getting validation that shouldnt? it would make it a whole lot easie to decipher the problem then – RhysW Jun 11 '12 at 15:35

3 Answers3

0

That's where ViewModels comes. Instead of directly binding the domain model with the views, you should create view models over them and bind to the views.

VJAI
  • 32,167
  • 23
  • 102
  • 164
  • I appreciate your input, however it does not really answer my question. If possible, I would like to keep my current architecture intact and stick with one "domain" model, as you put it. Suggesting I leverage ViewModels does not directly address the issue I'm having with partial validation. – user1449244 Jun 11 '12 at 15:49
  • If you already have an architecture in place and you can't change for view models you have to go for custom model binders, there are plenty of threads in SO reg. that. – VJAI Jun 11 '12 at 15:52
0

If you are not required to put validation on the EF models directly then you can use the MetadataType to do partial validation as needed. Take a look at my long example here on stackoverflow.

Community
  • 1
  • 1
Erik Philips
  • 53,428
  • 11
  • 128
  • 150
  • Ah yes, I saw that explanation earlier in my search before I asked this question. It is very thorough and quite detailed, but alas it does not directly answer my question. I understand the concept of breaking things down and handling validation via interface classes, but I'm really interested in finding the answer to partial validation as a whole. – user1449244 Jun 11 '12 at 16:44
  • Are you required to put validation on the EF models directly? – Erik Philips Jun 11 '12 at 16:46
  • Not necessarily, but that is just how I have things structured currently. – user1449244 Jun 11 '12 at 17:11
0

Thanks for the input all. However, I was able to obtain a solution in a very simple way. By placing the following code in the HttpPost element of the first View...

if (ModelState.IsValidField("FirstField") && ModelState.IsValidField("SecondField")) return RedirectToAction ("NameOfAction", model); else return View();

...I was able to achieve partial field validation. However, this field-specific approach will ONLY work provided the Submit button on the first View has class "cancel" and the additional validation errors that are generated (for the fields that are NOT present on the first View) are manually cleared before the above if statement. To do this, use:

ModelState["FieldName"].Errors.Clear();

No major change in architecure. No partial Views. No handing off unvalidated Data.

Works very well...

NOTE: If the second View loads with validation errors, use:

ModelState.Clear();

in the Action where the second View is initially called. This will make the second View load clean and error free, while still showing the validation errors later upon final form submission.

user1449244
  • 53
  • 1
  • 5