Client-side validation issue with Lookup

Mar 2, 2011 at 10:29 AM
Edited Mar 2, 2011 at 11:05 AM

Hi,

I have an issue with the client side validations and lookups due switching from "MicrosoftMvcJQueryValidation.js" and "HtmlHelper.UnobtrusiveJavaScriptEnabled = false;" to "https://ajax.aspnetcdn.com/ajax/mvc/3.0/jquery.validate.unobtrusive.min.js" and "HtmlHelper.UnobtrusiveJavaScriptEnabled = true;" ... The switched this due to the necessity to have Ajax Form.

My _Layout.cshtml inludes:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" type="text/javascript"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.9/jquery-ui.min.js" type="text/javascript"></script>
<script src="https://ajax.aspnetcdn.com/ajax/jQuery.Validate/1.7/jQuery.Validate.min.js" type="text/javascript"></script>
<script src="https://ajax.aspnetcdn.com/ajax/mvc/3.0/jquery.validate.unobtrusive.min.js" type="text/javascript"></script>

<script type="text/javascript" src="@Url.Content("~/Scripts/jquery.form.js")"></script>

<script type="text/javascript" src="@Url.Content("~/Scripts/Awesome.js")"></script>

 

My view with the issue is:

@model Models.SearchModel

@{ Html.EnableClientValidation(); }
@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>@Resources.Global.Main_BreadCrumb_Selection</legend>
        @Html.EditorFor(model => model.FromStation)
        @Html.EditorFor(model => model.ToStation)
        @Html.EditorFor(model => model.FromDate)
        @Html.EditorFor(model => model.ToDate)

</fieldset>
}

The From- and To- Lookups look like:

<%@ Page Language="C#" MasterPageFile="Template.Master" Inherits="System.Web.Mvc.ViewPage" %>

<asp:Content ID="Content1" ContentPlaceHolderID="Data" runat="server">
    <%= Html.Lookup(ViewData.TemplateInfo.GetFullHtmlFieldName(""), ViewData.TemplateInfo.FormattedModelValue, cancelText: Resources.Global.Lookup_Cancel, title: Resources.Global.Lookup_DepartureStationSelection, data: new Dictionary<string, string> { { "ToStation", "toStationEva" } })%>
</asp:Content>

and

<%@ Page Language="C#" MasterPageFile="Template.Master" Inherits="System.Web.Mvc.ViewPage" %>

<asp:Content ID="Content1" ContentPlaceHolderID="Data" runat="server">
    <%= Html.Lookup(ViewData.TemplateInfo.GetFullHtmlFieldName(""), ViewData.TemplateInfo.FormattedModelValue, cancelText: Resources.Global.Lookup_Cancel, title: Resources.Global.Lookup_ArrivalStationSelection, data: new Dictionary<string, string> { { "FromStation", "fromStationEva" } })%>
</asp:Content>

The SearchModel looks like:

public class SearchModel
    {

[Required(ErrorMessageResourceType = typeof (Global), ErrorMessageResourceName = "MainModel_FromStationRequired"
            )]
        [Display(ResourceType = typeof (Global), Name = "MainModel_FromStation")]
        [UIHint("FromLookup")]
        [Lookup(Controller = "FromStationLookup")]
        public string FromStation { get; set; }

[Required(ErrorMessageResourceType = typeof (Global), ErrorMessageResourceName = "MainModel_ToStationRequired")]
        [Display(ResourceType = typeof (Global), Name = "MainModel_ToStation")]
        [UIHint("ToLookup")]
        [Lookup(Controller = "ToStationLookup")]
        public string ToStation { get; set; }

[Required(ErrorMessageResourceType = typeof (Global), ErrorMessageResourceName = "MainModel_FromDateRequired")]
        [Display(ResourceType = typeof (Global), Name = "MainModel_FromDate")]
        public DateTime FromDate { get; set; }

[Display(ResourceType = typeof (Global), Name = "MainModel_ToDate")]
        public DateTime? ToDate { get; set; }

}

For the FromDate Property of the model (where no lookup is used), the required field validation is shown on the client side, whereas on the FromStation and ToStation Properties of the model (lookups used) no client side validation is shown.  Do u have any idea why is that so?

Tnx in advance!

Mar 2, 2011 at 12:13 PM
Edited Mar 2, 2011 at 12:16 PM

i guess because the mvc helpers put additional attributes to the <inputs for the validation and my lib doesn't

try editing Lookup.ascx

and put this:

<%=Html.Hidden(Model.Prop,Model.Value,new{@id = o}) %>

instead of

<input type="hidden" id="<%=o %>" name="<%=Model.Prop %>" value="<%=Model.Value %>" />