4

I have 4 forms in my asp.net mvc view. I have enabled client side validation on each by put <% Html.EnableClientValidation(); %> Above Html.BeginForm() of each form. The issue is that regardless of the fact that I've specified ID's for the forms the first form on the page gets validated whenever I click submit of the other forms.

Is this usage supported or am I doing something wrong?

ntombela
  • 1,357
  • 2
  • 19
  • 31

3 Answers3

1

this may help

                <%=Html.ValidationMessageFor(m => ((RegistrationFormModel)m.Data).Email, null, new { id = "registration_Email" })%>
imlarry
  • 46
  • 2
  • that is you just specifies a custom id for the field insead of the generated one. Woks fine. – imlarry Jun 25 '10 at 09:22
  • Thanks, this got me out of a bind when using the same form in a partial for both adding new data and editing existing. just added a simple check to see if the primary key was 0 (ie, new data), and added a custom id for those. – Jamie M Apr 27 '11 at 15:19
0

Make sure you have validation messages for the properties. Unless you have a validation message or (ValidateFor()), the property isn't added to the set of elements validated on form submission.

See this question for more info.

Community
  • 1
  • 1
tvanfosson
  • 524,688
  • 99
  • 697
  • 795
  • I'm using Html.ValidationMessageFor – ntombela Mar 20 '10 at 17:11
  • Client Side validation works with one form but as soon as I add more forms every time I click submit on the other forms the first form on the page will be validated. – ntombela Mar 20 '10 at 17:14
  • Hmmm. Looks like it only generates a single set of validation metadata. You might need to use some alternate client-side validation. – tvanfosson Mar 20 '10 at 19:40
0

MVC2 fully supports the setup that you're looking for, my guess is that you're applying this to something like displaying a registration form and a login form on the same page?

If so you just need each form to have independent property names, i.e.

LoginModel would have a Username property and RegistrationModel would have a RegistrationUsername.

Not a great example there, but what's probably happening is that the validation is firing cross form because your properties have the same name.

dove
  • 20,469
  • 14
  • 82
  • 108