views:

67

answers:

2

My layout is much simplified if I can only render parts of it when I need to display my validation summary.

The problem is that you can't use IsValid as it will throw if accessed before validation has occurred.

ViewData.ModelState.IsValid is false by default, so that won't work.

The only thing I've found that works is

<%if (this.ViewData.ModelState.Values.Where(x => x.Errors.Count > 0).Count() > 0)
  {%>

this kinda sucks. I'm looking for a better version of this. Is it out there?

+2  A: 

Have you checked out the validation example in the NerdDinner tutorial yet?

Integrating Validation and Business Rule Logic with Model Classes http://nerddinnerbook.s3.amazonaws.com/Part3.htm

If you are trying to validate before submitting the page to the controller, you are better off doing that with jQuery.

jQuery plugin: Validation
http://bassistance.de/jquery-plugins/jquery-plugin-validation/

Robert Harvey
This isn't validation; this is, from within the view, determining if there are validation errors. Its a subtle difference, I know. I'll def check out the nerd dinner stuff. I know that the validation plugin won't help me in this situation, as I already use it.
Will
I think I see what you are trying to do. I guess the real question is, why do you think your way of checking ModelState sucks? It looks pretty solid to me.
Robert Harvey
My target is my posterior, yet I'm going through my elbow to get there. It smells.
Will
+2  A: 

What Rob said. I'd check out xVal for a great way to tie your model validation to jquery validation with little added effort.

Wyatt Barnett
I'll check it out, but I'm not wondering how to validate, I'm wondering how to tell from within the view if there are validation errors.
Will
xVal will handle that by wiring up the jquery validation and also helping you stuff the errors into the `ModelState` where you can then use the standard controls (eg: `Html.ValidationSummary`).
Wyatt Barnett
Right, but I need to know in the view, so I can not render something. If I wait until the browser, I'm having issues with layout that would be greatly simplified by not rendering the troublesome parts in the first place.
Will