views:

1058

answers:

3

I'm trying to use MVC2 client-side validation in a partial view that is rendered via $.get. However, the client validation isn't working. I'm not quite sure what the deal is.

[Required(ErrorMessage = "Email is required")]
public string Email { get; set; }

<% using ( Ajax.BeginForm( new AjaxOptions { Confirm = "You sure?" } ) ) { %>
<%: Html.TextBoxFor( m => m.Email, new { @class = "TextBox150" } )%>
<%= Html.ValidationMessageFor( m => m.Email )%>
<input type="submit" value="Add/Save" style="float: right;" />
<% } %>

I'm not doing anything special to render the the partial view. Just putting the html into a div and showing it in a modal popup.

On a side note, does anyone know if it's possible to submit the form with client validation without a submit button?

+1  A: 

MVC2 client-side validation relies on some additional javascript being rendered on the page that encodes the validation rules from the model. I think you can get this from a partial view, though I haven't tried, as long as you include the EnableClientValidation call in your partial view just prior to beginning the form. Make sure that you have the MVC client javascript included in the main view.

<% Html.EnableClientValidation(); %>
<% using ( Ajax.BeginForm( new AjaxOptions { Confirm = "You sure?" } ) ) { %>
<%: Html.TextBoxFor( m => m.Email, new { @class = "TextBox150" } )%>
<%= Html.ValidationMessageFor( m => m.Email )%>
<input type="submit" value="Add/Save" style="float: right;" />
<% } %>

I usually use MVC2 model-based validation in conjunction with the jQuery validation plugin using the MVC/jQuery glue javascript from the MVCFutures code. I know that this works on form submission, whether the submit is triggered via a submit button or javascript. All of my "buttons" are really links that trigger form submission via javascript and it works for me. I presume this is true of the standard MVC validation scripts, but haven't used them.

tvanfosson
@tvanfosson sorry I forgot to include that in the code snippet. I do have the following right above my ajax.begin form:<% Html.EnableClientValidation(); %><% Html.ValidationSummary(); %>
devlife
@devlife -- I just noticed that it's being submitted via AJAX. I don't know if the validation logic fires when the form is submitted via AJAX -- since the submit event is aborted by the AJAX script so it can do it asynchronously. You might need to trigger the validation manually in an OnBegin method. Time to fire up Firebug using the debug versions of the javascript and see what actually gets executed, I think.
tvanfosson
A: 

Some time ago I wrote a post about this issue, you might use it as start point in your situation (you will have to modify the place where you call the functions and pass correct element to process): http://tpeczek.blogspot.com/2010/04/making-aspnet-mvc-2-client-side.html

tpeczek
A: 

Here is my solution to make any dynamically loaded JavaScript (including MVC2 Client Validation) seamlessly work:

http://adammcraventech.wordpress.com/2010/06/11/asp-net-mvc2-ajax-executing-dynamically-loaded-javascript/

adammcraven