Search code examples
ajaxasp.net-mvc-3unobtrusive-validationasp.net-mvc-partialviewclient-side-validation

MVC3 with Ajax partial view - OnBegin validation not performed


In an MVC3 application I have a view rendering two Ajax partial views: a Create partial view and a List one. In the Create partial view I try to fire client validation on OnBegin event, but this does not happen.

The error messages I get, when I force errors, are the one indicated in the model annotations, and not the ones I define in the validation function called OnBegin (e.g.:

  • leaving the event place empty, I get "The event place is required" (annotation) instead of "Where did it take place?" (FieldsValidation function),

  • for the event description I get "The event description is required" (annotation) instead of "Tell us sth about it!" (FieldsValidation function)

  • and the validation summary message sais "Error on creation. Please retry after corrections." instead of "Save failed. Please correct errors and retry."(FieldsValidation function)).

I tried to put a breakpoint in the FieldsValidation function, but it was never reached. Just to make sure, I also tried to call OnBegin a function displaying an alert (after the test I restored the original call to FieldsValidation function):

    function toDoOnBegin() { alert("This is onBegin"); }

The alert never displays!

If all fields are properly filled in, the event is correctly created and displayed in the event list (the other partial view). The problem is uniquely the function call OnBegin.

Please explain me what did I do wrong, did I miss to include something or what else is it?

Thank you truly.

Web.config

<appSettings>
    <add key="webpages:Version" value="1.0.0.0" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>

Model:

    public partial class Event
{
    public int client_ID { get; set; }

    [Required(ErrorMessage="The event date is required")]
    [DataType(DataType.Date, ErrorMessage="The event date is invalid")]
    public System.DateTime event_date { get; set; }

    [Required(ErrorMessage="The event place is required")]
    public string event_place{ get; set; }

    [Required(ErrorMessage="The event description is required")]
    public string event_description{ get; set; }
}

_Layout.cshtml :

Style sheets

<link href="@Url.Content("~/Content/styles.css")" rel="stylesheet" type="text/css" />
<link href="@Url.Content("~/Content/nivo-slider.css")" rel="stylesheet" type="text/css" media="screen"/>
<link href="@Url.Content("~/Content/themes/redmond/jquery-ui-1.8.18.custom.css")" rel="stylesheet" type="text/css" />
<link href="@Url.Content("~/Content/jquery.timepicker.css")" rel="stylesheet" type="text/css" />  
<link href="@Url.Content("~/Content/jquery.alerts.css")" rel="stylesheet" type="text/css" media="screen"/>

jQueries for datetimepicker plugin needed on several other pages

<script src="@Url.Content("~/Scripts/jquery-1.7.1.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery-ui-1.8.18.custom.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.ui.core.min.js")"  type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.ui.slider.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.ui.datepicker.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.ui.datepicker-fr.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery-ui-timepicker-addon.js")" type="text/javascript"></script>

jQueries for other application components

<script src="@Url.Content("~/Scripts/jquery.alerts.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/OJscript.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.nivo.slider.pack.js")" type="text/javascript"></script>

jQueries for validation

<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>

Index.cshtml

@model MyApp.ViewModels.EventViewModel

@{
    ViewBag.Title = "Events";
}

<div id="divUpdateable">
    @Html.Partial("_Create", Model)
</div>
<div id="divList">
    @Html.Partial("_ListEvents", Model.EventsList)
</div>

_Create.cshtml

@model MyApp.ViewModels.EventViewModel

<script type="text/javascript">

    function FieldsValidation() {
        $("#idformCreate").validate({

            errorContainer: $($('div.validation-summary-errors')),
            errorLabelContainer: $("ul", $('div.validation-summary-errors')),
            wrapper: 'li',
            errorClass: "input-validation-error2",

            rules:
                {
                    'Event.event_date':
                    {
                        required: true
                    },
                    'Event.event_place':
                    {
                        required: true
                    },
                    'Event.event_description':
                    {
                        required: true
                    }
                },
            messages:
                {
                    'Event.event_date':
                    {
                        required: "Date is required"
                    },
                    'Event.event_place':
                    {
                        required: "Where did it take place?"
                    },
                    'Event.event_description':
                    {
                        required: "Tell us sth about it!"
                    }
                }

        }).form();

        if (!$("#idformCreate").valid()) {
            $("div.validation-summary-errors span").html("Save failed. Please correct errors and retry.");
            return false;
        }

        return true;
    }
</script>

@using (Ajax.BeginForm("Create", "Event", new { clientID = Model.Event.client_ID }, new AjaxOptions
{
    UpdateTargetId = "divUpdateable",
    OnBegin = "FieldsValidation",
    InsertionMode = InsertionMode.Replace,
    OnSuccess = "getForm('" + Url.Action("ListEvents", "Event", null) + "','divList')"
}, new { id = "idformCreate" }))
{
    @Html.ValidationSummary(false, "Error on creation. Please retry after corrections.")
    <p>@Html.ValidationMessage("_FORM")</p>
    <div class="validation-summary-errors">
        <span></span>
        <ul>
        </ul>
    </div>
    <fieldset style="width: 800px; line-height: 1.4em;">
        <legend>Add an event</legend>
        <table style="width: 100%; padding-bottom: 0; padding-top: 0; border: 0;">
            <tr>
                <td style="width: 90px; border: 0;">
                    @Html.LabelFor(model => model.Event.event_date)
                </td>
                <td style="width: 200px; border: 0;">
                    @Html.EditorFor(model => model.Event.event_date, new { @class = "date" })
                    @Html.ValidationMessageFor(model => model.Event.event_date, "*")
                    @Html.HiddenFor(model => model.Event.client_ID)
                </td>
            </tr>
            <tr>
                <td style="width: 80px; border: 0; text-align: right;">
                    @Html.LabelFor(model => model.Event.event_place)
                </td>
                <td style="width: 120px; border: 0;">
                    @Html.DropDownList("Event.event_place", Model.PlacesList, "Where?", new { @style = "width: 100px;" })
                    @Html.ValidationMessageFor(model => model.Event.event_place, "")
                </td>
            </tr>
            <tr>
                <td style="width: 90px; border: 0;">
                    @Html.LabelFor(model => model.Event.event_description)
                </td>
                <td style="width: 600px; border: 0;" colspan="5">
                    @Html.TextAreaFor(model => model.Event.event_description, new { @class = "input_txt_nofloat", @style = "width:590px" })
                    @Html.ValidationMessageFor(model => model.Event.event_description, "*")
                </td>
            </tr>
            <tr style="height: 64px;">
                <td colspan="2" style="text-align: center;">
                    <br />
                    <br />
                    <input type="submit" value="Save" class="submit" style="width: 280px" />
                 </td>
            </tr>
        </table>
    </fieldset>
}

Solution

  • Here is the answer: (with a great thanks for BrockAllen)

    If you are doing your own client-side validation then you should remove jquery.validate.unobtrusive.min.js. This is MVC's client side validation code and they will conflict if both are using jquery.validate.

    Hopefully, this will help other people too.