views:

1778

answers:

3

I want as well as Client Side Validation as Server Side Validation. I realized this as the following:

Model: ( The model has a DataModel(dbml) which contains the Test class )

namespace MyProject.TestProject
{
    [MetadataType(typeof(TestMetaData))]
    public partial class Test
    {

    }

    public class TestMetaData
    {
        [Required(ErrorMessage="Please enter a name.")]
        [StringLength(50)]
        public string Name { get; set; }
    }
}

Controller is nothing special.

The View:

<% Html.EnableClientValidation(); %>
<% using (Ajax.BeginForm("Index", "Test", FormMethod.Post, 
            new AjaxOptions {}, new { enctype = "multipart/form-data" }))
   {%>
   <%= Html.AntiForgeryToken()%>
    <fieldset>
        <legend>Widget Omschrijving</legend>
        <div>
            <%= Html.LabelFor(Model => Model.Name) %>
            <%= Html.TextBoxFor(Model => Model.Name) %>
            <%= Html.ValidationMessageFor(Model => Model.Name) %>
        </div>
    </fieldset>
    <div>
        <input type="submit" value="Save" />
    </div>
 <% } %>

To make this all work I added also references to js files:

<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/jquery-1.4.1.min.js" type="text/javascript"></script>

Eventually it has to work, but it doesnt work 100%: It does validates with no page refresh after pressing the button. It also does "half" Client Side Validation. Only when you type some text into the textbox and then backspace the typed text. The Client Side Validation appears. But when I try this by tapping between controls there's no Client Side Validation.

Do I miss some reference or something? (I use Asp.Net MVC 2 RTM)

+1  A: 

I thought I was missing something too, but I do know that the jQuery validation plug in doesn't react to mouseEnter/mouseLeave. According to the documentation for that plug in (at the plug in page - look for the section labeled "A few things to look for when playing around with the demo"):

Before a field is marked as invalid, the validation is lazy: Before submitting the form for the first time, the user can tab through fields without getting annoying messages - he won't get bugged before he had the chance to actually enter a correct value

I assume it's the same rule with MVC 2 client-side validation. I can't be sure of that because the documentation doesn't say anything but "it works on my machine" :).

I went with the jQuery validation route, which is supported in MVC Futures (note that the documentation for that is non-existent). To use it, you just need to replace the script tags for MicrosoftAjax.js, MicrosoftMvcAjax.js and MicrosoftMvcValidation.js files with these two tags:

<script src="/js/jquery.validate.js" type="text/javascript"></script>
<script src="/js/MicrosoftMvcJQueryValidation.js" type="text/javascript"></script>

replacing the path with yours.

I also found the need to upgrade to the latest version of jquery.validate.js from the developer's page, because we are using a later version of jQuery (1.4) here.

Hope that helps.

Tim Rourke
I modified my code with what you describe, but no improvements.I use:jQuery 1.4.2jQuery-validate 1.7MicrosoftMvcJQueryValidation no versionNumber availabe.I also changed the reference inside the MicrosoftMvcJQueryValidation .js file to /// <reference path="jquery-1.4.2.js" /> , but still no improvements.
Farrell
+1  A: 

hm..., it's late to answer but try this:

<script src="<%= Url.Content("ScriptFile.js") %>" type="text/javascript"></script>
Sadegh
+3  A: 

Change the order of the loaded javascript...

<script src="../../Scripts/jquery-1.4.1.min.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>

Had the exact same problem and this cleared it up for me...

Andrew Flanagan