Error using JQuery validation on DropDownLists
Hi I am having a problem with jquery validation on drop down lists. I am posting my code. But when I run it, I am getting the following error:
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: CS0103: The name 'dropdownlist1' does not exist in the current context
Source Error:
|
Line 79: $("#form1").validate({ Line 80: rules: { Line 81: '<%=dropdownlist1.ClientID%>': { Line 82: selectNone: true Line 83: },
|
- <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<NIC.Sikkim.ERMS.Presentation.ViewModels.DistrictViewModel>" %>
<asp:Content ID="Title" ContentPlaceHolderID="TitleContent" runat="server">
Create District
</asp:Content>
<asp:Content ID="Main" ContentPlaceHolderID="MainContent" runat="server">
<h2>
Create District</h2>
<% using (Html.BeginForm("DistrictIndex", "ControlData", FormMethod.Post, new { id = "form1" }))
{%>
<%--<%: Html.ValidationSummary(true) %>--%>
<fieldset>
<legend>Fields</legend>
<div class="editor-label">
<%: Html.LabelFor(model => model.District.StateId)%>
</div>
<div class="editor-field">
<%: Html.DropDownListFor(model => model.District.StateId, Model.States, "--Select Option--", new{ id="dropdownlist1"} )%>
<%: Html.ValidationMessageFor(model => model.District.StateId)%>
</div>
<div class="editor-label">
<%:Html.LabelFor(model => model.District.DistrictNo)%>
</div>
<div class="editor-field">
<%:Html.TextBoxFor(model => model.District.DistrictNo)%>
<%: Html.ValidationMessageFor(model => model.District.DistrictNo)%>
</div>
<div class="editor-label">
<%:Html.LabelFor(model => model.District.DistrictCensusNo)%>
</div>
<div class="editor-field">
<%:Html.TextBoxFor(model => model.District.DistrictCensusNo)%>
<%: Html.ValidationMessageFor(model => model.District.DistrictCensusNo)%>
</div>
<div class="editor-label">
<%:Html.LabelFor(model => model.District.DistrictName)%>
</div>
<div class="editor-field">
<%:Html.TextBoxFor(model => model.District.DistrictName)%>
<%: Html.ValidationMessageFor(model => model.District.DistrictName)%>
</div>
<div class="editor-label">
<%:Html.LabelFor(model => model.District.DistrictHq)%>
</div>
<div class="editor-field">
<%:Html.TextBoxFor(model => model.District.DistrictHq)%>
<%: Html.ValidationMessageFor(model => model.District.DistrictHq)%>
</div>
<div class="editor-label">
<%:Html.LabelFor(model => model.District.DistrictHqPostOffd)%>
</div>
<div class="editor-field">
<%: Html.DropDownListFor(model => model.District.DistrictHqPostOffd, Model.PostOffices, "Select Option", new { id = "dropdownlist2" })%>
<%: Html.ValidationMessageFor(model => model.District.DistrictHqPostOffd)%>
</div>
<div class="editor-label">
<%:Html.LabelFor(model => model.District.DistrictHdStdCode)%>
</div>
<div class="editor-field">
<%:Html.TextBoxFor(model => model.District.DistrictHdStdCode)%>
<%: Html.ValidationMessageFor(model => model.District.DistrictHdStdCode)%>
</div>
<%:Html.HiddenFor(model => model.District.CreatedBy, new { @Value = Page.User.Identity.Name.ToString() })%>
<%:Html.HiddenFor(model => model.District.CreatedDate, new { @Value = String.Format("{0:g}", DateTime.Now) })%>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
<% } %>
<div>
|<%:Html.ActionLink("Back to Districts","DistrictIndex",new {stateId=Request.QueryString["stateId"]}) %>
|<%: Html.ActionLink("Back to States", "Index") %>|
</div>
<%--JQUERY SCRIPT FOR FORM VALIDATION--%>
<script type="text/javascript">
$().ready(function () {
$("#form1").validate({
rules: {
'<%=dropdownlist1.ClientID%>': {
selectNone: true
},
DistrictNo: {
maxlength: 5
},
DistrictCensusNo: {
maxlength: 10
},
DistrictName: {
maxlength: 50
},
DistrictHq: {
maxlength: 50
},
'<%=dropdownlist2.ClientID%>': {
selectNone: true
},
DistrictHdStdCode: {
maxlength: 7
}
},
messages: {
StateId: {
required: "Please select an option."
},
DistrictNo: {
maxlength: "DistrictNo cannot be more than 5 characters in length."
},
DistrictCensusNo: {
maxlength: "DistrictCensusNo cannot be more than 10 characters in length."
},
DistrictName: {
maxlength: "DistrictName cannot be more than 50 characters in length."
},
DistrictHq: {
maxlength: "DistrictHq cannot be more than 50 characters in length."
},
DistrictHdStdCode: {
maxlength: "DistrictHdStdCode cannot be more than 7 characters in length."
}
}
});
});
//$("#dropdownlist1").validate();
$.validator.addMethod('selectNone',
function(value, element) {
return this.optional (element) ||
(value.indexOf("Select Option") == -1);
}, "Please select an option.");
</script>
</asp:Content>