locked
EnableClientValidation dosen't work with Ajax.BeginForm RRS feed

  • Question

  • User-1339422861 posted

    hi

    when i using Html.BeginForm  in view the EnableClientValidation is working

    but after using Ajax.BeginForm the EnableClientValidation dosen't work.

    why?

    Wednesday, April 28, 2010 4:00 AM

Answers

  • User-1935546128 posted

    Well I can't give you any general answer, because generally it works with Ajax.BeginForm (you can download sample application from here  and see it for yourself if you don't belive https://tpeczek.svn.codeplex.com/svn/trunk/AsynchronousFormExamplev2/). So the question is why it doesn't work for you. Please provide some of your code so we can take a look at it.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 28, 2010 4:27 AM
  • User-1935546128 posted

    Hi. I have spent some time on solving problems mentioned here, so here it is:

     

    @rezaiy.ali

    I managed to recreate your problem on one of my old projects. After migration to ASP.NET MVC 2 RTM the client side validation wasn't working for Ajax.BeginForm. The reason was in javascripts, there are not updated automaticly. So you should take out new scripts from templates (default location for Visual Web Developer 2010 is ''C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\VWDExpress\ProjectTemplatesCache\CSharp\Web\1033\MvcWebApplicationProjectTemplatev2.0.cs.zip\Scripts") and replace those you have in your project (all of them). That should help.

     

    @vazavi

    I have confirmed problem you have noticed. After few experiments I managed to find a solution and described it here: http://tpeczek.blogspot.com/2010/04/making-aspnet-mvc-2-client-side.html. I hope it will help you.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 29, 2010 6:25 PM
  • User-1935546128 posted

    Are you sure you replaced all the needed scripts and with the correct version? I can mail you correct one's if you not sure.  You should also check if you browser haven't cached the old scripts (my does it all the time) - just hit F5 few times. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, April 30, 2010 2:28 AM

All replies

  • User-1935546128 posted

    Well I can't give you any general answer, because generally it works with Ajax.BeginForm (you can download sample application from here  and see it for yourself if you don't belive https://tpeczek.svn.codeplex.com/svn/trunk/AsynchronousFormExamplev2/). So the question is why it doesn't work for you. Please provide some of your code so we can take a look at it.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 28, 2010 4:27 AM
  • User-1339422861 posted

    this is my view

    	<% Html.EnableClientValidation(); %>
    
            <% using (Ajax.BeginForm("AddProject", "Agency", new AjaxOptions
               {
                   OnSuccess = "Flash",
                   UpdateTargetId = "Message",
                   InsertionMode = InsertionMode.Replace
               }))
               {%>
                  
            <div class="editor-label">
                <%= Html.LabelFor(model => model.OrderDate) %>
            </div>
            <div class="editor-field">
                <%= Html.TextBox("OrderDate",BentaAccounting.Classes.GenralClasses.FarsiDate.GetDate(), new { style = "width:100px", ReadOnly=true })%>
                <%= Html.ValidationMessageFor(model => model.OrderDate) %>
            </div>
            <div class="editor-label">
                <%= Html.LabelFor(model => model.Taskmaster) %>
            </div>
            <div class="editor-field">
                <%= Html.TextBoxFor(model => model.Taskmaster, new { style = "width:250px" })%>
                <%= Html.ValidationMessageFor(model => model.Taskmaster) %>
            </div>
       <p>
                <input type="submit" value="Save" />
            </p>
            <% } %>

    and these are my Scripts

        <script src="../../Scripts/jquery-1.4.2.js" type="text/javascript"></script>
        <script src="../../Scripts/MicrosoftAjax.js" type="text/javascript"></script>
        <script src="../../Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script>
        <script src="../../Scripts/MicrosoftMvcValidation.js" type="text/javascript"></script>
        <script src="../../Scripts/BentaScript.js" type="text/javascript"></script>



     

     

    Wednesday, April 28, 2010 5:08 AM
  • User-1935546128 posted

    Are you sure you are using ASP.NET MVC 2 RTM? There was a known bug with Html.EnableClientValidation and Ajax.BeginForm in ASP.NET MVC 2 Preview 2.

    Wednesday, April 28, 2010 5:19 AM
  • User-1339422861 posted

    i am using MVC 2 RC and .Net 4

    Wednesday, April 28, 2010 5:39 AM
  • User-1005219520 posted

    Please upgrade to MVC2 RTM and don't post questions on RC or beta products when there is a RTM version. 

    Wednesday, April 28, 2010 12:42 PM
  • User-1880009398 posted

    Well I can't give you any general answer, because generally it works with Ajax.BeginForm (you can download sample application from here  and see it for yourself if you don't belive https://tpeczek.svn.codeplex.com/svn/trunk/AsynchronousFormExamplev2/). So the question is why it doesn't work for you. Please provide some of your code so we can take a look at it.

    Well, client validation works only until you reload a portion of the page using ajax. Using your example, replace

    <% } else { %>
    

     


    with

    <% }; { %>

    on AccountInformationForm.ascx. Now, register a user, and once the view is returned, try to register a user again. You'll see that the page skips client-side validation.

     

     

    Wednesday, April 28, 2010 5:20 PM
  • User-1935546128 posted

    Making that kind of change in this sample would make it a very bad sample ;). If you want to keep your form in the view, then you should not reload it with Ajax (you should send only the info message and inject it somewhere on the page). But I can see this problem in scenarios, where you need to inject completly new form into your page. I will take a look into it tommorow and see if there is any kind of workaround.

    Wednesday, April 28, 2010 7:23 PM
  • User-1339422861 posted

    now i am using MVC 2 RTM and VS 2010 Released

    but my problem isn't solution.

    Thursday, April 29, 2010 2:46 AM
  • User-1935546128 posted

    Hi. I have spent some time on solving problems mentioned here, so here it is:

     

    @rezaiy.ali

    I managed to recreate your problem on one of my old projects. After migration to ASP.NET MVC 2 RTM the client side validation wasn't working for Ajax.BeginForm. The reason was in javascripts, there are not updated automaticly. So you should take out new scripts from templates (default location for Visual Web Developer 2010 is ''C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\VWDExpress\ProjectTemplatesCache\CSharp\Web\1033\MvcWebApplicationProjectTemplatev2.0.cs.zip\Scripts") and replace those you have in your project (all of them). That should help.

     

    @vazavi

    I have confirmed problem you have noticed. After few experiments I managed to find a solution and described it here: http://tpeczek.blogspot.com/2010/04/making-aspnet-mvc-2-client-side.html. I hope it will help you.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 29, 2010 6:25 PM
  • User-1339422861 posted

    hi tpeczek

    thanks for replay

    i replaced those scripts to my project but it didn't work again.

     

    Friday, April 30, 2010 1:10 AM
  • User-1935546128 posted

    Are you sure you replaced all the needed scripts and with the correct version? I can mail you correct one's if you not sure.  You should also check if you browser haven't cached the old scripts (my does it all the time) - just hit F5 few times. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, April 30, 2010 2:28 AM
  • User-1339422861 posted

    thanks thanks thanks

    now it's working

    thanks again

     

    Friday, April 30, 2010 3:01 AM
  • User1919435213 posted

    tpeczek, can you email me the scripts please? web [-a-] sde [-dot-] at

    I have the following issue. I have an action link (Ajax.ActionLink) which loads a partial view into a div. This partial view contains a form (Ajax.BeginForm) which again posts its field to an action controller. And this form requires some validation which I want to have using client side validation. Using <% Html.EnableClientValidation(); %> doesn't work to validate my data in the partial view. I hope the new scripts will help to solve my problem.

    Tuesday, May 18, 2010 5:17 AM
  • User-1935546128 posted

    Here you can download files which contains functions I have added:

    https://tpeczek.svn.codeplex.com/svn/trunk/MVC/AsynchronousFormExamplev2/AsynchronousFormExamplev2/Scripts/MicrosoftMvcJQueryValidation.js

    https://tpeczek.svn.codeplex.com/svn/trunk/MVC/AsynchronousFormExamplev2/AsynchronousFormExamplev2/Scripts/MicrosoftMvcValidation.js

    Please remember also, that those functions have to be manually called in OnSuccess handler when loading content via AJAX (as described in my blog post).

     

    Tuesday, May 18, 2010 6:17 AM
  • User687043422 posted

    The link for the example is not working.  

    https://tpeczek.svn.codeplex.com/svn/trunk/AsynchronousFormExamplev2/

    404 - File or directory not found.

    The resource you are looking for might have been removed, had its name changed, or is temporarily unavailable.

    Thursday, June 3, 2010 3:33 PM
  • User-1935546128 posted

    Link has changed, here is correct one:

    https://tpeczek.svn.codeplex.com/svn/trunk/MVC/AsynchronousFormExamplev2/ 

    Thursday, June 3, 2010 4:09 PM
  • User687043422 posted

    Thanks for the updated link.  Would it be possible to get a link to the referenced project.

    ..\..\Lib.Web.Mvc\Lib.Web.Mvc\Lib.Web.Mvc.csproj

    I have the project loaded with the exception of that include.

     

    Thursday, June 3, 2010 4:54 PM
  • User-1935546128 posted

    Sure thing:

    https://tpeczek.svn.codeplex.com/svn/trunk/MVC/Lib.Web.Mvc/

    You can also browse entire repository in browser (the ".." link goes to parent directory) or svn client. You can also download complete packages from here:

    http://tpeczek.codeplex.com/ 

    Thursday, June 3, 2010 5:22 PM