LeftyX
LeftyX

Reputation: 35587

EnableClientValidation with multiple forms

I am trying to use the Client-side Validation with jQuery in ASP.NET MVC (2). I've found this great example

which works fine but I am having problems when I try to use a custom name for my form. It seems that the EnableClientValidation methods uses the default form name "form0" to inject the client script and doesn't support any other name.
Here's a code sample of what I am trying to do:

<%=Html.ValidationSummaryJQuery("Please fix these errors.", "id", "BPValidationID")%>
<% Html.EnableClientValidation()%>
<%  Using (Html.BeginForm("Edit", "Home", New With {.Id = Model.Code}, FormMethod.Post, New With {.id = "EditForm"}))%>
        <% ViewContext.FormContext.ValidationSummaryId = "BPValidationID"%>
        <%=Html.AntiForgeryToken("AF-BP-SPED-token")%>
    <fieldset>
        <legend>Fields</legend>
        <div class="editor-label">
            <%=Html.LabelFor(Function(m) m.Name)%>
        </div>
        <div class="editor-field">
            <%=Html.TextBoxFor(Function(m) m.Name)%>
            <%=Html.ValidationMessageFor(Function(m) m.Name, "*")%>
        </div>
        <p>
            <input type="submit" name="submitButton" value="Save" />
        </p>
    </fieldset>            
    <%End Using%>

Is there any chance for me to use a FORM name, in case I want to use multiple forms on my page?

Thanks for any help,

Alberto

Upvotes: 0

Views: 567

Answers (2)

LeftyX
LeftyX

Reputation: 35587

I've changed the extension method ValidationSummaryJQuery; this method now receives an object instead of a IDictionary.

Upvotes: 0

Craig Stuntz
Craig Stuntz

Reputation: 126557

I presume you mean form id? Forms don't have names. Anyway, EnableClientValidation works fine with custom form ids. If you're having a problem, look at the generated HTML/JS.

This is real-world, working code:

<% Html.EnableClientValidation(); %> 
<% using (Html.BeginForm(null, 
                         null, 
                         new RouteValueDictionary{{ "Id", Html.ModelId() }, { "ReturnUrl", ViewData.Eval("ReturnUrl") }}, 
                         FormMethod.Post, 
                         new Dictionary<string, object> { { "id", "editForm" } })) { %>
    <div id="row1">
        <%: Html.EditorForModel() %>
    </div>

The rendered form is:

<form action="/Snipped/Url" id="editForm" method="post">
    <div id="row1">
        <input ...

Upvotes: 1

Related Questions