本文介绍了为什么我的ajax返回错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我正在创建一个regitration表单,但我不知道它为什么会返回错误 这是我的表格模型 public partial class ApplicationFormFarmersSolarPower { public int Id {获得; set ; } public string UniqueId { get ; set ; } // [必填(ErrorMessage =请选择区。)] // public string District {get;组; } // [必需(ErrorMessage =请选择Tehsil。 )] // public string Tehsil {get;组; } // [必需(ErrorMessage =请选择SubTehsil。 )] // public string SubTehsil {get;组; } public string DistrictName {获得; set ; } public string TehsilName { get ; set ; } public string SubTehsilName { get ; set ; } public string Village {获得; set ; } [StringLength( 20 ,MinimumLength = 1 ,ErrorMessage = 请输入1到20个字符的名称。)] [必需(ErrorMessage = 请输入名称)] [显示(名称= *申请人姓名)] public string NameOfApplicant { get ; set ; } [StringLength( 30 )] [必需(ErrorMessage = 请输入土地所有者姓名)] [显示(名称= *土地业主/业主名称)] public string NameOfLandOwners { get ; set ; } [StringLength( 30 )] [显示(名称= *地址)] [必需(ErrorMessage = 请输入注册地址)] [RegularExpression( @ [^< - >!*] + $,ErrorMessage = 此类字符(< ,!,>,*)不允许)] public string ResidentAddress { get ; set ; } [StringLength( 6 ,ErrorMessage = 不应超过6个字符。)] [必需(ErrorMessage = 请输入密码)] [RegularExpression( @ ^ [0 -9] + $,ErrorMessage = 无效的邮政编码)] public Nullable< long> PinCode { get ; set ; } [StringLength( 10 ,ErrorMessage = 不应超过10个字符。)] [必需(ErrorMessage = 请输入手机号码。)] // [RegularExpression(@ / [1-9] {1} \d {9} /,ErrorMessage =无效的手机号码。)] [显示(名称= 手机号码)] public Nullable< long> MobileNumber { get ; set ; } [必需(ErrorMessage = 请输入电子邮件ID。) ] [StringLength( 100 ,ErrorMessage = 不应超过100个字符。)] [DataType(DataType.EmailAddress,ErrorMessage = 无效的电子邮件地址)] [RegularExpression( @ \ w + ([ - +。'] \w +)* @ \ w +([ - 。] \ w +)* \。\ w +([ - 。] \w +)*,ErrorMessage = 无效的电子邮件地址)] [显示(名称= 电子邮件ID)] public string EmailId {获得; set ; } [必需(ErrorMessage = 请选择最近的站点。) ] [显示(名称= *最近的66 KVA PSPCL子站)] public string 最近的站点{ get ; set ; } [显示(名称= *变电站距离(以米为单位))] [必需(ErrorMessage = 请选择变电站距离。) ] public string DistanceOfSubstation { get ; set ; } [显示(名称= 土地面积)] [必需(ErrorMessage = 请选择土地面积)] public string AreaOfLand { get ; set ; } public string ProposedPlantCapacity { get ; set ; } [必需(ErrorMessage = 请选择区。)] public Nullable< int> DistrictId { get ; set ; } [必需(ErrorMessage = 请选择Tehsil。)] public Nullable< int> TehsilId { get ; set ; } [必需(ErrorMessage = 请选择SubTehsil。)] public Nullable< int> SubTehsilId { get ; set ; } } 我的家庭控制器 public ActionResult Index1() { List< District> allDistrict = new 列表< District>(); 列表< Tehsil> allTehsil = new 列表< Tehsil>(); List< SubTehsil> allSubTehsil = new 列表< SubTehsil>(); 使用(FarmerBDContext db = new FarmerBDContext()) { allDistrict = db.Districts.ToList(); } ViewBag.DistrictId = new SelectList(allDistrict, DistrictId, DistrictName); ViewBag.TehsilId = new SelectList(allTehsil, TehsilId, Tehsilname); ViewBag.SubTehsilId = new SelectList(allSubTehsil, SubTehsilId, SubTehsilName); return 查看(); } [HttpPost] [ValidateAntiForgeryToken] public ActionResult详细信息(ApplicationFormFarmersSolarPower app) { List< District> allDistrict = new 列表< District>(); 列表< Tehsil> allTehsil = new 列表< Tehsil>(); List< SubTehsil> allSubTehsil = new 列表< SubTehsil>(); 使用(FarmerBDContext db = new FarmerBDContext()) { allDistrict = db.Districts.ToList(); if (app!= null && app.DistrictId > 0 ) { allTehsil = db.Tehsils.Where(x = > x.TehsilId.Equals(app.DistrictId))。OrderBy(a = > a.TehsilName)。ToList (); } if (app!= null && app.TehsilId > 0 ) { allSubTehsil = db.SubTehsils.Where(x = > x.SubTehsilId.Equals(app.TehsilId))。OrderBy(a = > a .SubTehsilName).ToList(); } } ViewBag.DistrictId = new SelectList(allDistrict, DistrictId, DistrictName ,app.DistrictId); ViewBag.TehsilId = new SelectList(allTehsil, TehsilId, Tehsilname,app.TehsilId); ViewBag.SubTehsilId = new SelectList(allSubTehsil, SubTehsilId, SubTehsilName,app.SubTehsilId); if (ModelState.IsValid) { using (FarmerBDContext db = new FarmerBDContext()) { db.ApplicationFormFarmersSolarPowers.Add(app); db.SaveChanges(); ModelState.Clear(); app = null ; ViewBag.Message = SuccessFul; } } else { ViewBag.Message = UnSuccessFul; } return 查看(app); } [HttpGet] public JsonResult GetTehsil( string districtID = ) { List< Tehsil> allTehsil = new 列表< Tehsil>(); int ID = 0 ; if ( int .TryParse(districtID, out ID)) { 使用(FarmerBDContext db = new FarmerBDContext()) { allTehsil = db.Tehsils.Where(a = > a.DistrictId.Equals(ID))。 OrderBy(a = > a.TehsilName)。ToList(); } } if (Request.IsAjaxRequest()) { return new JsonResult { Data = allTehsil, JsonRequestBehavior = JsonRequestBehavior .AllowGet }; } 其他 { 返回 new JsonResult { Data = 无效请求, JsonRequestBehavior = JsonRequestBehavior.AllowGet }; } } [HttpGet] public JsonResult GetSubTehsil(字符串 tehsilId = ) {列表与LT; SubTehsil> allSubTehsil = new 列表< SubTehsil>(); int ID = 0 ; if ( int .TryParse(tehsilId, out ID)) { 使用(FarmerBDContext db = new FarmerBDContext()) { allSubTehsil = db.SubTehsils.Where(a = > a.TehsilId.Equals(ID))。 OrderBy(a = > a.SubTehsilName)。ToList(); } } if (Request.IsAjaxRequest()) { return new JsonResult { Data = allSubTehsil, JsonRequestBehavior = JsonRequestBehavior .AllowGet }; } 其他 { 返回 new JsonResult { Data = 无效请求, JsonRequestBehavior = JsonRequestBehavior.AllowGet }; } } 我的IndexView @ model LastTry.Models.ApplicationFormFarmersSolarPower @ { ViewBag.Title =Ind1ex; Layout =〜/ Views / Shared / _LayoutFarmer.cshtml; } < script src = 〜/ Scripts / jquery -1.8.2.min.js > < / script > < script src = 〜/ Scripts / jquery.validate.min.js > < / script > < script src = 〜/ Scripts / jquery.validate.unobtrusive.js > < / script > < script src = 〜/ Scripts /jquery.unobtrusive-ajax.min.js\"> < / script > < h2 > I nd1ex < / h2 > @using(Html.BeginForm(Details,Home,FormMethod.Post)) { @ Html.AntiForgeryToken() @ Html.ValidationSummary (true) < fieldset > < 图例 > ApplicationFormFarmersSolarPower < / legend > @if(ViewBag.Message!= null) { < div style = border:1px solid black > @ ViewBag.Message < / div > } < div class = editor-label > @ Html.LabelFor(model => model.UniqueId) < / div > < div class = editor-field > @ Html.EditorFor(model => model.UniqueId) @ Html.ValidationMessageFor(model => model.UniqueId) < / div > < div class = 编辑标签 & gt; @ Html.LabelFor(model => model.Village) < / div > < div class = editor-field > @ Html.EditorFor(model => model.Village) @ Html.ValidationMessageFor(model => model.Village) < / div > < div class = 编辑标签 > @ Html.LabelFor(model => model.NameOfApplicant) < / div > < div class = editor-field > @ Html.EditorFor(model => model.NameOfApplicant) @ Html.ValidationMessageFor(model => model.NameOfApplicant) < / div > < div class = 编辑标签 > @ Html.LabelFor(model => model.NameOfLandOwners) < / div > < div class = editor-field > @ Html.EditorFor(model => model.NameOfLandOwners) @ Html.ValidationMessageFor(model => model.NameOfLandOwners) < / div > < div class = 编辑标签 > @ Html.LabelFor(model => model.ResidentAddress) < / div > < div class = editor-field > @ Html.EditorFor(model => model.ResidentAddress) @ Html.ValidationMessageFor(model => model.ResidentAddress) < / div > < div class = 编辑标签 > @ Html.LabelFor(model => model.PinCode) < / div > < div class = editor-field > @ Html.EditorFor(model => model.PinCode) @ Html.ValidationMessageFor(model => model.PinCode) < / div > < div class = 编辑标签 > @ Html.LabelFor(model => model.MobileNumber) < / div > < div class = editor-field > @ Html.EditorFor(model => model.MobileNumber) @ Html.ValidationMessageFor(model => model.MobileNumber) < / div > < div class = 编辑标签 > @ Html.LabelFor(model => model.EmailId) < / div > < div class = editor-field > @ Html.EditorFor(model => model.EmailId) @ Html.ValidationMessageFor(model => model.EmailId) < / div > < div class = 编辑标签 > @ Html.LabelFor(model => model.NearestStation) < / div > < div class = editor-field > @ Html.EditorFor(model => model.NearestStation) @ Html.ValidationMessageFor(model => model.NearestStation) < / div > < div class = 编辑标签 > @ Html.LabelFor(model => model.DistanceOfSubstation) < / div > < div class = editor-field > @ Html.EditorFor(model => model.DistanceOfSubstation) @ Html.ValidationMessageFor(model => model.DistanceOfSubstation) < / div > < div class = 编辑标签 > @ Html.LabelFor(model => model.AreaOfLand) < / div > < div class = editor-field > @Html.EditorFor(model => model.AreaOfLand) @Html.ValidationMessageFor(model => model.AreaOfLand) </div > <div class=\" editor-label\"> @Html.LabelFor(model => model.ProposedPlantCapacity) </div> <div class=\"editor-field\"> @Html.EditorFor(model => model.ProposedPlantCapacity) @Html.ValidationMessageFor(model => model.ProposedPlantCapacity) </div> <div class=\"editor-la bel\"> @Html.LabelFor(model => model.DistrictName) </div> <div class=\"editor-field\"> @Html.DropDownListFor(model => model.DistrictName, @ViewBag.DistrictId as SelectList, \"Select District\", new { @class = \"ddl\" }) @Html.ValidationMessageFor(model => model.DistrictId) </div> <div class=\"editor-label\"> @Html.LabelFor(model => model.TehsilName) </div> <div class=\"editor-field\"> @Html.DropDownListFor(model => model.TehsilName, @ViewBag.TehsilId as SelectList, \"Select Tehsil\", new { @class = \"ddl\" }) @Html.ValidationMessageFor(model => model.TehsilId) </div> <div class =\"editor-label\"> @Html.LabelFor(model => model.SubTehsilName) </div> <div class=\"editor-field\"> @Html.DropDownListFor(model => model.SubTehsilName, @ViewBag.SubTehsilId as SelectList, \"Select SubTehsil\", new { @class = \"ddl\" }) @Html.ValidationMessageFor(model => model.SubTehsilId) </div> <p> <input type=\"submit\" value=\"Details\" /> </p> </fieldset> } <div> @Html.ActionLink(\"Back to List\", \"Index\") < /div> @*@section Scripts { @Scripts.Render(\"~/bundles/jqueryval\")*@ <script type=\"text/javascript\"> //$(document).ready(function () { $(document).ready(function () { $(\"#DistrictName\").change(function () { var dId = parseInt($(\"#Distric tName\").val()); if (!isNaN(dId)) { var ddState = $(\"#TeshilName\"); ddState.empty(); ddState.append($(\"<option></option\").val(\"\").html(\"Select State\")); $.ajax({ url: \"@Url.Action(\"GetTehsil\", \"Home\")\", type: \"GET\", data: { \"districtID\": dId }, dataType: \"json\", success: function (data) { $.each(data, function (i, val) { ddState.append( $(\"<option></option>\"). val(val.TehsilId).html(val.TehsilName) ); }); }, error: function () { alert(\"Error!\"); } }); } }); $(\"#TehsilName\").change(function () { var tehsilID = parseInt($('#TehsilName').val()); if (!isNaN(tehsilID)) { var ddstate = $(\"#SubTeshilName\"); ddstate.empty(); ddstate.append($(\"<option></option>\").val(\"\").html(\"Select SubTehsil\")); $.ajax({ url: \"@Url.Action(\"GetSubTehsil\",\"Home\")\", type: \"GET\", data: { \"tehsilId\": tehsilID }, dataType: \"json\", success: function (data) { $.each(data, function (i, val) { ddstate.append( $(\"<option></option>\").val(val.SubTehsilId).html(val.SubTehsilName) ); }); }, error: function (data) { alert(\"Error!\"); } }); } }); }); </script> When i run it in Debug Mode it is returning the value from GetTehsil action in Home Controller, i even alerted the selected value which is coming fine but i do not know why it is giving error any idea? My three classes for DropDownList, these classes are generated by Entity framework public partial class District { public District() { this.tbTehsils = new HashSet<Tehsil>(); } public int DistrictId { get; set ; } public string DistrictName { get; set ; } public virtual ICollection<Tehsil> tbTehsils { get; set ; } } public partial class Tehsil { public Tehsil() { this.tbSubTehsils = new HashSet<SubTehsil>(); } public int TehsilId { get; set ; } public string TehsilName { get; set ; } public int DistrictId { get; set ; } public virtual District tbDistrict { get; set ; } public virtual ICollection<SubTehsil> tbSubTehsils { get; set ; } } public partial class SubTehsil { public int SubTehsilId { get; set ; } public string SubTehsilName { get; set ; } public Nullable< int> TehsilId { get; set ; } public virtual Tehsil tbTehsil { get; set ; } } 解决方案 \", ErrorMessage = \"Such Chars (<,!,>,*) are not allowed\")] public string ResidentAddress { get; set; } [StringLength(6, ErrorMessage = \"Should not be more than 6 chars.\")] [Required(ErrorMessage = \"Please enter pincode\")] [RegularExpression(@\"^[0-9]+\", ErrorMessage = \"Invalid zipcode\")] pu blic Nullable<long> PinCode { get; set ; } [StringLength(10, ErrorMessage = \"Should not be more than 10 characters.\")] [Required(ErrorMessage = \"Please enter mobile number.\")] //[RegularExpression(@\"/[1-9]{1}\d{9}/\", ErrorMessage = \"Invalid mobile number.\")] [Display(Name = \"Mobile Number\")] public Nullable<long> MobileNumber { get; set ; } [Required(ErrorMessage = \"Please enter email id.\")] [StringLength(100, ErrorMessage = \"Should not be more than 100 chars.\")] [DataType(DataType.EmailAddress, ErrorMessage = \"Invalid email address\")] [RegularExpression(@\"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*\", ErrorMessage = \"Invalid email address\")] [Display(Name = \"Email Id\")] public string EmailId { 获得; set ; } [Required(ErrorMessage = \"Please select Nearest Station.\")] [Display(Name = \"*Nearest 66 KVA PSPCL Sub Station\")] public string NearestStation { get; set ; } [Display(Name = \"*Distance of Substation (in meters)\")] [Required(ErrorMessage = \"Please select Distance of Substation.\")] public string DistanceOfSubstation { get; set ; } [Display(Name = \"Area of Land\")] [Required(ErrorMessage = \"Please select Area of Land\")] public string AreaOfLand { get; set ; } public string ProposedPlantCapacity { get; set ; } [Required(ErrorMessage = \"Please select District.\")] public Nullable<int> DistrictId { get; set ; } [Required(ErrorMessage = \"Please select Tehsil.\")] public Nullable<int> TehsilId { get; set ; } [Required(ErrorMessage = \"Please select SubTehsil.\")] public Nullable<int> SubTehsilId { get; set ; } } My Home Controller public ActionResult Index1() { List<District> allDistrict = new List<District>(); List<Tehsil> allTehsil = new 列表< Tehsil>(); List<SubTehsil> allSubTehsil = new List<SubTehsil>(); using (FarmerBDContext db = new FarmerBDContext()) { allDistrict = db.Districts.ToList(); } ViewBag.DistrictId = new SelectList(allDistrict, \"DistrictId\", \"DistrictName\"); ViewBag.TehsilId = new SelectList(allTehsil, \"TehsilId\", \"Tehsilname\"); ViewBag.SubTehsilId = new SelectList(allSubTehsil, \"SubTehsilId\", \"SubTehsilName\"); return 查看(); } [HttpPost] [ValidateAntiForgeryToken] public ActionResult Details(ApplicationFormFarmersSolarPower app) { List<District> allDistrict = new List<District>(); List<Tehsil> allTehsil = new 列表< Tehsil>(); List<SubTehsil> allSubTehsil = new List<SubTehsil>(); using (FarmerBDContext db = new FarmerBDContext()) { allDistrict = db.Districts.ToList(); if (app != null && app.DistrictId > 0) { allTehsil = db.Tehsils.Where(x => x.TehsilId.Equals(app.DistrictId)).OrderBy(a => a.TehsilName).ToList(); } if (app != null && app.TehsilId > 0) { allSubTehsil = db.SubTehsils.Where(x => x.SubTehsilId.Equals(app.TehsilId)).OrderBy(a => a.SubTehsilName).ToList(); } } ViewBag.DistrictId = new SelectList(allDistrict, \"DistrictId\", \"DistrictName\", app.DistrictId); ViewBag.TehsilId = new SelectList(allTehsil, \"TehsilId\", \"Tehsilname\", app.TehsilId); ViewBag.SubTehsilId = new SelectList(allSubTehsil, \"SubTehsilId\", \"SubTehsilName\", app.SubTehsilId); if (ModelState.IsValid) { using (FarmerBDContext db = new FarmerBDContext()) { db.ApplicationFormFarmersSolarPowers.Add(app); db.SaveChanges(); ModelState.Clear(); app = null; ViewBag.Message = \"SuccessFul\"; } } else { ViewBag.Message = \"UnSuccessFul\"; } return View(app); } [HttpGet] public JsonResult GetTehsil(string districtID = \"\") { List<Tehsil> allTehsil = new 列表< Tehsil>(); int ID = 0 ; if (int.TryParse(districtID, out ID)) { using (FarmerBDContext db = new FarmerBDContext()) { allTehsil = db.Tehsils.Where(a => a.DistrictId.Equals(ID)).OrderBy(a => a.TehsilName).ToList(); } } if (Request.IsAjaxRequest()) { return new JsonResult { Data = allTehsil, JsonRequestBehavior = JsonRequestBehavior.AllowGet }; } 其他 { 返回 new JsonResult { Data = 无效请求, JsonRequestBehavior = JsonRequestBehavior.AllowGet }; } } [HttpGet] public JsonResult GetSubTehsil(string tehsilId = \"\") { List<SubTehsil> allSubTehsil = new List<SubTehsil>(); int ID = 0 ; if (int.TryParse(tehsilId, out ID)) { using (FarmerBDContext db = new FarmerBDContext()) { allSubTehsil = db.SubTehsils.Where(a => a.TehsilId.Equals(ID)).OrderBy(a => a.SubTehsilName).ToList(); } } if (Request.IsAjaxRequest()) { return new JsonResult { Data = allSubTehsil, JsonRequestBehavior = JsonRequestBehavior.AllowGet }; } 其他 { 返回 new JsonResult { Data = 无效请求, JsonRequestBehavior = JsonRequestBehavior.AllowGet }; } } My IndexView @model LastTry.Models.ApplicationFormFarmersSolarPower @{ ViewBag.Title = \"Ind1ex\"; Layout = \"~/Views/Shared/_LayoutFarmer.cshtml\"; } <script src=\"~/Scripts/jquery-1.8.2.min.js\"></script> <script src=\"~/Scripts/jquery.validate.min.js\"></script> <script src=\"~/Scripts/jquery.validate.unobtrusive.js\"></script> <script src=\"~/Scripts/jquery.unobtrusive-ajax.min.js\"></script> <h2>I nd1ex</h2> @using (Html.BeginForm(\"Details\", \"Home\", FormMethod.Post)) { @Html.AntiForgeryToken() @Html.ValidationSummary(true) <fieldset> <legend>ApplicationFormFarmersSolarPower</legend> @if (ViewBag.Message != null) { <div style=\"border:1px solid black\"> @ViewBag.Message </div> } <div class=\"editor-label\"> @Html.LabelFor(model => model.UniqueId) </div> <div class=\"editor-field\"> @Html.EditorFor(model => model.UniqueId) @Html.ValidationMessageFor(model => model.UniqueId) </div> <div class=\"editor-label\"& gt; @Html.LabelFor(model => model.Village) </div> <div class=\"editor-field\"> @Html.EditorFor(model => model.Village) @Html.ValidationMessageFor(model => model.Village) </div> <div class=\"editor-label\"> @Html.LabelFor(model => model.NameOfApplicant) </div> <div class=\"editor-field\"> @Html.EditorFor(model => model.NameOfApplicant) @Html.ValidationMessageFor(model => model.NameOfApplicant) </div> <div class=\"editor-label\"> @Html.LabelFor(model => model.NameOfLandOwners) </div> <div class=\"editor-field\"> @Html.EditorFor(model => model.NameOfLandOwners) @Html.ValidationMessageFor(model => model.NameOfLandOwners) </div> <div class=\"editor-label\"> @Html.LabelFor(model => model.ResidentAddress) </div> <div class=\"editor-field\"> @Html.EditorFor(model => model.ResidentAddress) @Html.ValidationMessageFor(model => model.ResidentAddress) </div> <div class=\"editor-label\"> @Html.LabelFor(model => model.PinCode) </div> <div class=\"editor-field\"> @Html.EditorFor(model => model.PinCode) @Html.ValidationMessageFor(model => model.PinCode) </div> <div class=\"editor-label\"> @Html.LabelFor(model => model.MobileNumber) </div> <div class=\"editor-field\"> @Html.EditorFor(model => model.MobileNumber) @Html.ValidationMessageFor(model => model.MobileNumber) </div> <div class=\"editor-label\"> @Html.LabelFor(model => model.EmailId) </div> <div class=\"editor-field\"> @Html.EditorFor(model => model.EmailId) @Html.ValidationMessageFor(model => model.EmailId) </div> <div class=\"editor-label\"> @Html.LabelFor(model => model.NearestStation) </div> <div class=\"editor-field\"> @Html.EditorFor(model => model.NearestStation) @Html.ValidationMessageFor(model => model.NearestStation) </div> <div class=\"editor-label\"> @Html.LabelFor(model => model.DistanceOfSubstation) </div> <div class=\"editor-field\"> @Html.EditorFor(model => model.DistanceOfSubstation) @Html.ValidationMessageFor(model => model.DistanceOfSubstation) </div> <div class=\"editor-label \"> @Html.LabelFor(model => model.AreaOfLand) </div> <div class=\"editor-field\"> @Html.EditorFor(model => model.AreaOfLand) @Html.ValidationMessageFor(model => model.AreaOfLand) </div> <div class=\"editor-label\"> @Html.LabelFor(model => model.ProposedPlantCapacity) </div> <div class=\"editor-field\"> @Html.EditorFor(model => model.ProposedPlantCapacity) @Html.ValidationMessageFor(model => model.ProposedPlantCapacity) </div> <div class=\"editor-la bel\"> @Html.LabelFor(model => model.DistrictName) </div> <div class=\"editor-field\"> @Html.DropDownListFor(model => model.DistrictName, @ViewBag.DistrictId as SelectList, \"Select District\", new { @class = \"ddl\" }) @Html.ValidationMessageFor(model => model.DistrictId) </div> <div class=\"editor-label\"> @Html.LabelFor(model => model.TehsilName) </div> <div class=\"editor-field\"> @Html.DropDownListFor(model => model.TehsilName, @ViewBag.TehsilId as SelectList, \"Select Tehsil\", new { @class = \"ddl\" }) @Html.ValidationMessageFor(model => model.TehsilId) </div> <div class =\"editor-label\"> @Html.LabelFor(model => model.SubTehsilName) </div> <div class=\"editor-field\"> @Html.DropDownListFor(model => model.SubTehsilName, @ViewBag.SubTehsilId as SelectList, \"Select SubTehsil\", new { @class = \"ddl\" }) @Html.ValidationMessageFor(model => model.SubTehsilId) </div> <p> <input type=\"submit\" value=\"Details\" /> </p> </fieldset> } <div> @Html.ActionLink(\"Back to List\", \"Index\") < /div> @*@section Scripts { @Scripts.Render(\"~/bundles/jqueryval\")*@ <script type=\"text/javascript\"> //(document).ready(function () {I am creating a regitration form but i do not know why it is returning errorThis is my model for formpublic partial class ApplicationFormFarmersSolarPower { public int Id { get; set; } public string UniqueId { get; set; } //[Required(ErrorMessage = "Please select District.")] //public string District { get; set; } //[Required(ErrorMessage = "Please select Tehsil.")] //public string Tehsil { get; set; } //[Required(ErrorMessage = "Please select SubTehsil.")] //public string SubTehsil { get; set; } public string DistrictName { get; set; } public string TehsilName { get; set; } public string SubTehsilName { get; set; } public string Village { get; set; } [StringLength(20, MinimumLength = 1, ErrorMessage = "Please enter a Name within 1 to 20 chars.")] [Required(ErrorMessage = "Please Enter Name")] [Display(Name = "*Name of Applicant")] public string NameOfApplicant { get; set; } [StringLength(30)] [Required(ErrorMessage = "Please Enter Name of Land Owner")] [Display(Name = "*Name of Land Owners / Owners")] public string NameOfLandOwners { get; set; } [StringLength(30)] [Display(Name = "*Address")] [Required(ErrorMessage = "Please Enter Registered Address")] [RegularExpression(@"[^<->!*]+$", ErrorMessage = "Such Chars (<,!,>,*) are not allowed")] public string ResidentAddress { get; set; } [StringLength(6, ErrorMessage = "Should not be more than 6 chars.")] [Required(ErrorMessage = "Please enter pincode")] [RegularExpression(@"^[0-9]+$", ErrorMessage = "Invalid zipcode")] public Nullable<long> PinCode { get; set; } [StringLength(10, ErrorMessage = "Should not be more than 10 characters.")] [Required(ErrorMessage = "Please enter mobile number.")] //[RegularExpression(@"/[1-9]{1}\d{9}/", ErrorMessage = "Invalid mobile number.")] [Display(Name = "Mobile Number")] public Nullable<long> MobileNumber { get; set; } [Required(ErrorMessage = "Please enter email id.")] [StringLength(100, ErrorMessage = "Should not be more than 100 chars.")] [DataType(DataType.EmailAddress, ErrorMessage = "Invalid email address")] [RegularExpression(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", ErrorMessage = "Invalid email address")] [Display(Name = "Email Id")] public string EmailId { get; set; } [Required(ErrorMessage = "Please select Nearest Station.")] [Display(Name = "*Nearest 66 KVA PSPCL Sub Station")] public string NearestStation { get; set; } [Display(Name = "*Distance of Substation (in meters)")] [Required(ErrorMessage = "Please select Distance of Substation.")] public string DistanceOfSubstation { get; set; } [Display(Name = "Area of Land")] [Required(ErrorMessage = "Please select Area of Land")] public string AreaOfLand { get; set; } public string ProposedPlantCapacity { get; set; } [Required(ErrorMessage = "Please select District.")] public Nullable<int> DistrictId { get; set; } [Required(ErrorMessage = "Please select Tehsil.")] public Nullable<int> TehsilId { get; set; } [Required(ErrorMessage = "Please select SubTehsil.")] public Nullable<int> SubTehsilId { get; set; } }My Home Controllerpublic ActionResult Index1() { List<District> allDistrict = new List<District>(); List<Tehsil> allTehsil = new List<Tehsil>(); List<SubTehsil> allSubTehsil = new List<SubTehsil>(); using (FarmerBDContext db = new FarmerBDContext()) { allDistrict = db.Districts.ToList(); } ViewBag.DistrictId = new SelectList(allDistrict, "DistrictId", "DistrictName"); ViewBag.TehsilId = new SelectList(allTehsil, "TehsilId", "Tehsilname"); ViewBag.SubTehsilId = new SelectList(allSubTehsil, "SubTehsilId", "SubTehsilName"); return View(); } [HttpPost] [ValidateAntiForgeryToken] public ActionResult Details(ApplicationFormFarmersSolarPower app) { List<District> allDistrict = new List<District>(); List<Tehsil> allTehsil = new List<Tehsil>(); List<SubTehsil> allSubTehsil = new List<SubTehsil>(); using (FarmerBDContext db = new FarmerBDContext()) { allDistrict = db.Districts.ToList(); if (app != null && app.DistrictId > 0) { allTehsil = db.Tehsils.Where(x => x.TehsilId.Equals(app.DistrictId)).OrderBy(a => a.TehsilName).ToList(); } if (app != null && app.TehsilId > 0) { allSubTehsil = db.SubTehsils.Where(x => x.SubTehsilId.Equals(app.TehsilId)).OrderBy(a => a.SubTehsilName).ToList(); } } ViewBag.DistrictId = new SelectList(allDistrict, "DistrictId", "DistrictName", app.DistrictId); ViewBag.TehsilId = new SelectList(allTehsil, "TehsilId", "Tehsilname", app.TehsilId); ViewBag.SubTehsilId = new SelectList(allSubTehsil, "SubTehsilId", "SubTehsilName", app.SubTehsilId); if (ModelState.IsValid) { using (FarmerBDContext db = new FarmerBDContext()) { db.ApplicationFormFarmersSolarPowers.Add(app); db.SaveChanges(); ModelState.Clear(); app = null; ViewBag.Message = "SuccessFul"; } } else { ViewBag.Message = "UnSuccessFul"; } return View(app); } [HttpGet] public JsonResult GetTehsil(string districtID = "") { List<Tehsil> allTehsil = new List<Tehsil>(); int ID = 0; if (int.TryParse(districtID, out ID)) { using (FarmerBDContext db = new FarmerBDContext()) { allTehsil = db.Tehsils.Where(a => a.DistrictId.Equals(ID)).OrderBy(a => a.TehsilName).ToList(); } } if (Request.IsAjaxRequest()) { return new JsonResult { Data = allTehsil, JsonRequestBehavior = JsonRequestBehavior.AllowGet }; } else { return new JsonResult { Data = "Not Valid request", JsonRequestBehavior = JsonRequestBehavior.AllowGet }; } } [HttpGet] public JsonResult GetSubTehsil(string tehsilId = "") { List<SubTehsil> allSubTehsil = new List<SubTehsil>(); int ID = 0; if (int.TryParse(tehsilId, out ID)) { using (FarmerBDContext db = new FarmerBDContext()) { allSubTehsil = db.SubTehsils.Where(a => a.TehsilId.Equals(ID)).OrderBy(a => a.SubTehsilName).ToList(); } } if (Request.IsAjaxRequest()) { return new JsonResult { Data = allSubTehsil, JsonRequestBehavior = JsonRequestBehavior.AllowGet }; } else { return new JsonResult { Data = "Not Valid request", JsonRequestBehavior = JsonRequestBehavior.AllowGet }; } }My IndexView@model LastTry.Models.ApplicationFormFarmersSolarPower@{ ViewBag.Title = "Ind1ex"; Layout = "~/Views/Shared/_LayoutFarmer.cshtml";}<script src="~/Scripts/jquery-1.8.2.min.js"></script><script src="~/Scripts/jquery.validate.min.js"></script><script src="~/Scripts/jquery.validate.unobtrusive.js"></script><script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script><h2>Ind1ex</h2>@using (Html.BeginForm("Details", "Home", FormMethod.Post)){ @Html.AntiForgeryToken() @Html.ValidationSummary(true) <fieldset> <legend>ApplicationFormFarmersSolarPower</legend> @if (ViewBag.Message != null) { <div style="border:1px solid black"> @ViewBag.Message </div> } <div class="editor-label"> @Html.LabelFor(model => model.UniqueId) </div> <div class="editor-field"> @Html.EditorFor(model => model.UniqueId) @Html.ValidationMessageFor(model => model.UniqueId) </div> <div class="editor-label"> @Html.LabelFor(model => model.Village) </div> <div class="editor-field"> @Html.EditorFor(model => model.Village) @Html.ValidationMessageFor(model => model.Village) </div> <div class="editor-label"> @Html.LabelFor(model => model.NameOfApplicant) </div> <div class="editor-field"> @Html.EditorFor(model => model.NameOfApplicant) @Html.ValidationMessageFor(model => model.NameOfApplicant) </div> <div class="editor-label"> @Html.LabelFor(model => model.NameOfLandOwners) </div> <div class="editor-field"> @Html.EditorFor(model => model.NameOfLandOwners) @Html.ValidationMessageFor(model => model.NameOfLandOwners) </div> <div class="editor-label"> @Html.LabelFor(model => model.ResidentAddress) </div> <div class="editor-field"> @Html.EditorFor(model => model.ResidentAddress) @Html.ValidationMessageFor(model => model.ResidentAddress) </div> <div class="editor-label"> @Html.LabelFor(model => model.PinCode) </div> <div class="editor-field"> @Html.EditorFor(model => model.PinCode) @Html.ValidationMessageFor(model => model.PinCode) </div> <div class="editor-label"> @Html.LabelFor(model => model.MobileNumber) </div> <div class="editor-field"> @Html.EditorFor(model => model.MobileNumber) @Html.ValidationMessageFor(model => model.MobileNumber) </div> <div class="editor-label"> @Html.LabelFor(model => model.EmailId) </div> <div class="editor-field"> @Html.EditorFor(model => model.EmailId) @Html.ValidationMessageFor(model => model.EmailId) </div> <div class="editor-label"> @Html.LabelFor(model => model.NearestStation) </div> <div class="editor-field"> @Html.EditorFor(model => model.NearestStation) @Html.ValidationMessageFor(model => model.NearestStation) </div> <div class="editor-label"> @Html.LabelFor(model => model.DistanceOfSubstation) </div> <div class="editor-field"> @Html.EditorFor(model => model.DistanceOfSubstation) @Html.ValidationMessageFor(model => model.DistanceOfSubstation) </div> <div class="editor-label"> @Html.LabelFor(model => model.AreaOfLand) </div> <div class="editor-field"> @Html.EditorFor(model => model.AreaOfLand) @Html.ValidationMessageFor(model => model.AreaOfLand) </div> <div class="editor-label"> @Html.LabelFor(model => model.ProposedPlantCapacity) </div> <div class="editor-field"> @Html.EditorFor(model => model.ProposedPlantCapacity) @Html.ValidationMessageFor(model => model.ProposedPlantCapacity) </div> <div class="editor-label"> @Html.LabelFor(model => model.DistrictName) </div> <div class="editor-field"> @Html.DropDownListFor(model => model.DistrictName, @ViewBag.DistrictId as SelectList, "Select District", new { @class = "ddl" }) @Html.ValidationMessageFor(model => model.DistrictId) </div> <div class="editor-label"> @Html.LabelFor(model => model.TehsilName) </div> <div class="editor-field"> @Html.DropDownListFor(model => model.TehsilName, @ViewBag.TehsilId as SelectList, "Select Tehsil", new { @class = "ddl" }) @Html.ValidationMessageFor(model => model.TehsilId) </div> <div class="editor-label"> @Html.LabelFor(model => model.SubTehsilName) </div> <div class="editor-field"> @Html.DropDownListFor(model => model.SubTehsilName, @ViewBag.SubTehsilId as SelectList, "Select SubTehsil", new { @class = "ddl" }) @Html.ValidationMessageFor(model => model.SubTehsilId) </div> <p> <input type="submit" value="Details" /> </p> </fieldset>}<div> @Html.ActionLink("Back to List", "Index")</div>@*@section Scripts { @Scripts.Render("~/bundles/jqueryval")*@<script type="text/javascript"> //$(document).ready(function () { $(document).ready(function () { $("#DistrictName").change(function () { var dId = parseInt($("#DistrictName").val()); if (!isNaN(dId)) { var ddState = $("#TeshilName"); ddState.empty(); ddState.append($("<option></option").val("").html("Select State")); $.ajax({ url: "@Url.Action("GetTehsil", "Home")", type: "GET", data: { "districtID": dId }, dataType: "json", success: function (data) { $.each(data, function (i, val) { ddState.append( $("<option></option>").val(val.TehsilId).html(val.TehsilName) ); }); }, error: function () { alert("Error!"); } }); } }); $("#TehsilName").change(function () { var tehsilID = parseInt($('#TehsilName').val()); if (!isNaN(tehsilID)) { var ddstate = $("#SubTeshilName"); ddstate.empty(); ddstate.append($("<option></option>").val("").html("Select SubTehsil")); $.ajax({ url: "@Url.Action("GetSubTehsil","Home")", type: "GET", data: { "tehsilId": tehsilID }, dataType: "json", success: function (data) { $.each(data, function (i, val) { ddstate.append( $("<option></option>").val(val.SubTehsilId).html(val.SubTehsilName) ); }); }, error: function (data) { alert("Error!"); } }); } }); });</script>When i run it in Debug Mode it is returning the value from GetTehsil action in Home Controller, i even alerted the selected value which is coming fine but i do not know why it is giving error any idea?My three classes for DropDownList, these classes are generated by Entity frameworkpublic partial class District { public District() { this.tbTehsils = new HashSet<Tehsil>(); } public int DistrictId { get; set; } public string DistrictName { get; set; } public virtual ICollection<Tehsil> tbTehsils { get; set; } }public partial class Tehsil { public Tehsil() { this.tbSubTehsils = new HashSet<SubTehsil>(); } public int TehsilId { get; set; } public string TehsilName { get; set; } public int DistrictId { get; set; } public virtual District tbDistrict { get; set; } public virtual ICollection<SubTehsil> tbSubTehsils { get; set; } }public partial class SubTehsil { public int SubTehsilId { get; set; } public string SubTehsilName { get; set; } public Nullable<int> TehsilId { get; set; } public virtual Tehsil tbTehsil { get; set; } } 解决方案 ", ErrorMessage = "Such Chars (<,!,>,*) are not allowed")] public string ResidentAddress { get; set; } [StringLength(6, ErrorMessage = "Should not be more than 6 chars.")] [Required(ErrorMessage = "Please enter pincode")] [RegularExpression(@"^[0-9]+", ErrorMessage = "Invalid zipcode")] public Nullable<long> PinCode { get; set; } [StringLength(10, ErrorMessage = "Should not be more than 10 characters.")] [Required(ErrorMessage = "Please enter mobile number.")] //[RegularExpression(@"/[1-9]{1}\d{9}/", ErrorMessage = "Invalid mobile number.")] [Display(Name = "Mobile Number")] public Nullable<long> MobileNumber { get; set; } [Required(ErrorMessage = "Please enter email id.")] [StringLength(100, ErrorMessage = "Should not be more than 100 chars.")] [DataType(DataType.EmailAddress, ErrorMessage = "Invalid email address")] [RegularExpression(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", ErrorMessage = "Invalid email address")] [Display(Name = "Email Id")] public string EmailId { get; set; } [Required(ErrorMessage = "Please select Nearest Station.")] [Display(Name = "*Nearest 66 KVA PSPCL Sub Station")] public string NearestStation { get; set; } [Display(Name = "*Distance of Substation (in meters)")] [Required(ErrorMessage = "Please select Distance of Substation.")] public string DistanceOfSubstation { get; set; } [Display(Name = "Area of Land")] [Required(ErrorMessage = "Please select Area of Land")] public string AreaOfLand { get; set; } public string ProposedPlantCapacity { get; set; } [Required(ErrorMessage = "Please select District.")] public Nullable<int> DistrictId { get; set; } [Required(ErrorMessage = "Please select Tehsil.")] public Nullable<int> TehsilId { get; set; } [Required(ErrorMessage = "Please select SubTehsil.")] public Nullable<int> SubTehsilId { get; set; } }My Home Controllerpublic ActionResult Index1() { List<District> allDistrict = new List<District>(); List<Tehsil> allTehsil = new List<Tehsil>(); List<SubTehsil> allSubTehsil = new List<SubTehsil>(); using (FarmerBDContext db = new FarmerBDContext()) { allDistrict = db.Districts.ToList(); } ViewBag.DistrictId = new SelectList(allDistrict, "DistrictId", "DistrictName"); ViewBag.TehsilId = new SelectList(allTehsil, "TehsilId", "Tehsilname"); ViewBag.SubTehsilId = new SelectList(allSubTehsil, "SubTehsilId", "SubTehsilName"); return View(); } [HttpPost] [ValidateAntiForgeryToken] public ActionResult Details(ApplicationFormFarmersSolarPower app) { List<District> allDistrict = new List<District>(); List<Tehsil> allTehsil = new List<Tehsil>(); List<SubTehsil> allSubTehsil = new List<SubTehsil>(); using (FarmerBDContext db = new FarmerBDContext()) { allDistrict = db.Districts.ToList(); if (app != null && app.DistrictId > 0) { allTehsil = db.Tehsils.Where(x => x.TehsilId.Equals(app.DistrictId)).OrderBy(a => a.TehsilName).ToList(); } if (app != null && app.TehsilId > 0) { allSubTehsil = db.SubTehsils.Where(x => x.SubTehsilId.Equals(app.TehsilId)).OrderBy(a => a.SubTehsilName).ToList(); } } ViewBag.DistrictId = new SelectList(allDistrict, "DistrictId", "DistrictName", app.DistrictId); ViewBag.TehsilId = new SelectList(allTehsil, "TehsilId", "Tehsilname", app.TehsilId); ViewBag.SubTehsilId = new SelectList(allSubTehsil, "SubTehsilId", "SubTehsilName", app.SubTehsilId); if (ModelState.IsValid) { using (FarmerBDContext db = new FarmerBDContext()) { db.ApplicationFormFarmersSolarPowers.Add(app); db.SaveChanges(); ModelState.Clear(); app = null; ViewBag.Message = "SuccessFul"; } } else { ViewBag.Message = "UnSuccessFul"; } return View(app); } [HttpGet] public JsonResult GetTehsil(string districtID = "") { List<Tehsil> allTehsil = new List<Tehsil>(); int ID = 0; if (int.TryParse(districtID, out ID)) { using (FarmerBDContext db = new FarmerBDContext()) { allTehsil = db.Tehsils.Where(a => a.DistrictId.Equals(ID)).OrderBy(a => a.TehsilName).ToList(); } } if (Request.IsAjaxRequest()) { return new JsonResult { Data = allTehsil, JsonRequestBehavior = JsonRequestBehavior.AllowGet }; } else { return new JsonResult { Data = "Not Valid request", JsonRequestBehavior = JsonRequestBehavior.AllowGet }; } } [HttpGet] public JsonResult GetSubTehsil(string tehsilId = "") { List<SubTehsil> allSubTehsil = new List<SubTehsil>(); int ID = 0; if (int.TryParse(tehsilId, out ID)) { using (FarmerBDContext db = new FarmerBDContext()) { allSubTehsil = db.SubTehsils.Where(a => a.TehsilId.Equals(ID)).OrderBy(a => a.SubTehsilName).ToList(); } } if (Request.IsAjaxRequest()) { return new JsonResult { Data = allSubTehsil, JsonRequestBehavior = JsonRequestBehavior.AllowGet }; } else { return new JsonResult { Data = "Not Valid request", JsonRequestBehavior = JsonRequestBehavior.AllowGet }; } }My IndexView@model LastTry.Models.ApplicationFormFarmersSolarPower@{ ViewBag.Title = "Ind1ex"; Layout = "~/Views/Shared/_LayoutFarmer.cshtml";}<script src="~/Scripts/jquery-1.8.2.min.js"></script><script src="~/Scripts/jquery.validate.min.js"></script><script src="~/Scripts/jquery.validate.unobtrusive.js"></script><script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script><h2>Ind1ex</h2>@using (Html.BeginForm("Details", "Home", FormMethod.Post)){ @Html.AntiForgeryToken() @Html.ValidationSummary(true) <fieldset> <legend>ApplicationFormFarmersSolarPower</legend> @if (ViewBag.Message != null) { <div style="border:1px solid black"> @ViewBag.Message </div> } <div class="editor-label"> @Html.LabelFor(model => model.UniqueId) </div> <div class="editor-field"> @Html.EditorFor(model => model.UniqueId) @Html.ValidationMessageFor(model => model.UniqueId) </div> <div class="editor-label"> @Html.LabelFor(model => model.Village) </div> <div class="editor-field"> @Html.EditorFor(model => model.Village) @Html.ValidationMessageFor(model => model.Village) </div> <div class="editor-label"> @Html.LabelFor(model => model.NameOfApplicant) </div> <div class="editor-field"> @Html.EditorFor(model => model.NameOfApplicant) @Html.ValidationMessageFor(model => model.NameOfApplicant) </div> <div class="editor-label"> @Html.LabelFor(model => model.NameOfLandOwners) </div> <div class="editor-field"> @Html.EditorFor(model => model.NameOfLandOwners) @Html.ValidationMessageFor(model => model.NameOfLandOwners) </div> <div class="editor-label"> @Html.LabelFor(model => model.ResidentAddress) </div> <div class="editor-field"> @Html.EditorFor(model => model.ResidentAddress) @Html.ValidationMessageFor(model => model.ResidentAddress) </div> <div class="editor-label"> @Html.LabelFor(model => model.PinCode) </div> <div class="editor-field"> @Html.EditorFor(model => model.PinCode) @Html.ValidationMessageFor(model => model.PinCode) </div> <div class="editor-label"> @Html.LabelFor(model => model.MobileNumber) </div> <div class="editor-field"> @Html.EditorFor(model => model.MobileNumber) @Html.ValidationMessageFor(model => model.MobileNumber) </div> <div class="editor-label"> @Html.LabelFor(model => model.EmailId) </div> <div class="editor-field"> @Html.EditorFor(model => model.EmailId) @Html.ValidationMessageFor(model => model.EmailId) </div> <div class="editor-label"> @Html.LabelFor(model => model.NearestStation) </div> <div class="editor-field"> @Html.EditorFor(model => model.NearestStation) @Html.ValidationMessageFor(model => model.NearestStation) </div> <div class="editor-label"> @Html.LabelFor(model => model.DistanceOfSubstation) </div> <div class="editor-field"> @Html.EditorFor(model => model.DistanceOfSubstation) @Html.ValidationMessageFor(model => model.DistanceOfSubstation) </div> <div class="editor-label"> @Html.LabelFor(model => model.AreaOfLand) </div> <div class="editor-field"> @Html.EditorFor(model => model.AreaOfLand) @Html.ValidationMessageFor(model => model.AreaOfLand) </div> <div class="editor-label"> @Html.LabelFor(model => model.ProposedPlantCapacity) </div> <div class="editor-field"> @Html.EditorFor(model => model.ProposedPlantCapacity) @Html.ValidationMessageFor(model => model.ProposedPlantCapacity) </div> <div class="editor-label"> @Html.LabelFor(model => model.DistrictName) </div> <div class="editor-field"> @Html.DropDownListFor(model => model.DistrictName, @ViewBag.DistrictId as SelectList, "Select District", new { @class = "ddl" }) @Html.ValidationMessageFor(model => model.DistrictId) </div> <div class="editor-label"> @Html.LabelFor(model => model.TehsilName) </div> <div class="editor-field"> @Html.DropDownListFor(model => model.TehsilName, @ViewBag.TehsilId as SelectList, "Select Tehsil", new { @class = "ddl" }) @Html.ValidationMessageFor(model => model.TehsilId) </div> <div class="editor-label"> @Html.LabelFor(model => model.SubTehsilName) </div> <div class="editor-field"> @Html.DropDownListFor(model => model.SubTehsilName, @ViewBag.SubTehsilId as SelectList, "Select SubTehsil", new { @class = "ddl" }) @Html.ValidationMessageFor(model => model.SubTehsilId) </div> <p> <input type="submit" value="Details" /> </p> </fieldset>}<div> @Html.ActionLink("Back to List", "Index")</div>@*@section Scripts { @Scripts.Render("~/bundles/jqueryval")*@<script type="text/javascript"> //(document).ready(function () { 这篇关于为什么我的ajax返回错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 07-25 13:26