views:

31

answers:

1

Hello,

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

+1  A: 

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 ...
Craig Stuntz
Yes, I meant FORM id, sorry for my mistake. As soon as I add the Html.EnableClientValidation() my FORM id becomes form0.
vandalo
It shouldn't. Mine doesn't. I'll update the answer with a real-world, working syntax example.
Craig Stuntz
You're right. It works fine in C#. I don't know what's wrong in Vb.Net but is seems that the problems is there. Thanks for your support Craig.
vandalo
I've changed the extension method ValidationSummaryJQuery and everything works fine now. This method now receives an object instead of a IDictionary<string, object>
vandalo
Hi Vandalo,I guess you figured out why it isn't working for you.Yes as you noticed, my HTML Helper Extension method accepts a Dictionary.I didn't like using the `object` and converting it to a `Dictionary` using Reflection.That's why I intentionally was using the `Dictionary`.Sorry I replied late, but I'm glad you figured it out.Maybe I'll add in a new extension method that accepts an `object`.-Soe
stun
Thanks for your reply, Stun. Yes, I guess using an IDictionary in VB.NET 9 and trying to call it with an object initializer is a bit of a pain.I've changed it with an object so I can call it like this: New With {.id = "BPValidationID"}
vandalo