1

I have login partial view in jquery dialog box.when I click on "Login" button with empty textboxes, error should be shown, but it goes with null value to server and get this error. I want to use client validation, but the partial view in dialog doesnt change to html input .... Also I try to use Unobtrusive , but I got varient error in IE9.

now , I have used these script :

 <script src="@Url.Content("~/Scripts/jquery-1.4.4.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery-ui.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")" type="text/javascript"></script>

what should i do for client validation without Unobtrusive ?

1 Answer 1

2

If you dont want to use the unobtrusive plugin, then you will need to manually configure the jquery validation plugin. There are multiple ways of doing this, I suggest you to start by the following link: http://docs.jquery.com/Plugins/Validation

But take into account that by not using the unobtrusive validation plugin you wont benefit from all of the infrastructure already in place regarding client validations in MVC 3, like progressive enhancement or having both client and server side validations based on data annotations.

The jquery.validate.unobtrusive script that comes with ASP MVC 3 is dependent on jquery version 1.5.1, which also comes with MVC 3. It will probably work with higher versions (I have used it with version 1.6.4) but I am not sure with lower versions like the 1.4.4. The jquery.validation script is also dependent on the jquery version. For example the jquery.validation 1.8 will work nicely with jquery 1.5.1, but will need to be upgraded to jquery.validation 1.10 if you are using jquery 1.6.4.

So from the default MVC 3 application, you could use the following scripts:

    <script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery-ui-1.8.11.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>

Make also sure that client side validation is enabled on your MVC application by adding these settings to the config file (which are set by default when a new MVC 3 application is created)

<configuration>
  ...
  <appSettings>
    ...
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
    ...
  </appSettings>

Finally take into account that the validate unobtrusive script will actually parse the document when the it is initially loaded. If you inspect the jquery.validate.unobtrusive.js file, you will see these lines at the end of the document:

$(function () {
    $jQval.unobtrusive.parse(document);
});

So anything loaded into the DOM after the page was initially loaded will not be handled by the unobtrusive validation, and will need to be manually parsed by you. This can be done by defining a function like the following, included in your page or a common javascript file:

function partialViewLoaded(request, status, partialViewId) {
    if (request.responseText != "") {
        //Hook validations on elements of the loaded view
        $.validator.unobtrusive.parse($(partialViewId));        
    }
}

That function will be called after loading the partial view using ajax. If you are using the MVC Ajax helper, you could set a complete callback like the following one (Assuming you are loading the view into an element with id "loginForm"):

@using(Ajax.BeginForm(new AjaxOptions{ 
                           UpdateTargetId = "loginForm", 
                           OnComplete = "partialViewLoaded(xhr, status, '#loginForm')" })){

If you are manually performing the ajax call using jquery, then you can also set the complete callback as in:

$.ajax({
     url: "yourURL",
     complete: function(xhr, status){
         partialViewLoaded(xhr, status, "#loginForm");
     }
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Daniel, your answering was so helpfull for me that i didn't know versions are important.:)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.