本文介绍了如何在mvc中返回JsonResult的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 [HttpPost] [ValidateAntiForgeryToken] public ActionResult create(n_userdata userdata) { 列表< n_country> allcountry = new List< n_country>(); List< n_state> allstate = new List< n_state>(); List< n_city> allcity = new List< n_city>(); using(nyalkaranEntities db = new nyalkaranEntities()) { allcountry = db.n_country .OrderBy(v => v.countryname)。ToList(); if(userdata!= null&& userdata.countryid> 0) { allstate = db.n_state.Where(a => a.countryid.Equals(userdata.countryid))。OrderBy(a => a.state).ToList(); } } ViewBag.countryid = new SelectList(allcountry,countryid ,countryname,userdata.countryid); ViewBag.stateid = new SelectList(allstate,stateid,statename,userdata.stateid); ViewBag。 cityid = new SelectList(allcity,cityid,cityname,userdata.cityid); if(ModelState.I sValid) { 使用(nyalkaranEntities db = new nyalkaranEntities()) { db.n_userdata。添加(userdata); db.SaveChanges(); ModelState.Clear(); userdata = null; ViewBag.messagee =sabmit sussfully done; } } else { ViewBag.messagee =falid please try agen leter; } return View(); } [HttpGet] public JsonResult (string countryid = ) { List< n_state> allstate = new List< n_state>(); int id = 0; if(int.TryParse(countryid,out id) ) { 使用(nyalkaranEntities db = new nyalkaranEntities()) { allstate = db.n_state .Where(a => a.countryid.Equals(id))。OrderBy(a => a.state).ToList(); } if (Request.IsAjaxRequest()) { 返回新的JsonResult { Data = allstate, JsonRequestBehavior = JsonRequestBehavior.AllowGet }; } else { 返回新的JsonResult { Data =no velid rqvest, JsonRequestBehavior = JsonRequestBehavior.DenyGet }; } } 这是我的观点和jqurey [HttpPost] [ValidateAntiForgeryToken] public ActionResult create(n_userdata userdata) { List<n_country> allcountry = new List<n_country>(); List<n_state> allstate = new List<n_state>(); List<n_city> allcity = new List<n_city>(); using (nyalkaranEntities db = new nyalkaranEntities()) { allcountry = db.n_country.OrderBy(v => v.countryname).ToList(); if(userdata !=null && userdata.countryid>0) { allstate = db.n_state.Where(a => a.countryid.Equals(userdata.countryid)).OrderBy(a => a.state).ToList(); } } ViewBag.countryid = new SelectList(allcountry, "countryid", "countryname",userdata.countryid); ViewBag.stateid = new SelectList(allstate, "stateid", "statename",userdata.stateid); ViewBag.cityid = new SelectList(allcity, "cityid", "cityname",userdata.cityid); if (ModelState.IsValid) { using(nyalkaranEntities db= new nyalkaranEntities()) { db.n_userdata.Add(userdata); db.SaveChanges(); ModelState.Clear(); userdata = null; ViewBag.messagee="sabmit sussesfully done"; } } else { ViewBag.messagee = "falid please try agen leter"; } return View(); } [HttpGet] public JsonResult (string countryid = "") { List<n_state> allstate = new List<n_state>(); int id = 0; if(int.TryParse(countryid, out id)) { using (nyalkaranEntities db = new nyalkaranEntities()) { allstate = db.n_state.Where(a => a.countryid.Equals(id)).OrderBy(a => a.state).ToList(); } if (Request.IsAjaxRequest()) { return new JsonResult { Data = allstate, JsonRequestBehavior = JsonRequestBehavior.AllowGet }; } else { return new JsonResult { Data = "no velid rqvest", JsonRequestBehavior = JsonRequestBehavior.DenyGet }; } }this is my view and jqurey @ Html.DropDownListFor(model => model.CountryID,@ ViewBag.countryid作为SelectList,选择国家) @ Html.ValidationMessageFor(model => model.CountryID) @Html.DropDownListFor(model => model.CountryID,@ViewBag.countryid as SelectList,"select country") @Html.ValidationMessageFor(model => model.CountryID) @ Html.LabelFor(model => model.StateID) @Html.LabelFor(model => model.StateID) @ Html.DropDownListFor(model => model.StateID,@ ViewBag。 stateid为SelectList,select state) @ Html.ValidationMessageFor(model => model.StateID) @Html.DropDownListFor(model => model.StateID, @ViewBag.stateid as SelectList,"select state") @Html.ValidationMessageFor(model => model.StateID) < input type =submitvalue =创建/> <input type="submit" value="Create" /> @ Html.ActionLink(返回列表 ,索引) @Html.ActionLink("Back to List", "Index") @section脚本{ @ Scripts.Render (〜/ bundles / jqueryval) < script type =text / javascript> ; $(文件).ready(function(){ //这是调用wen country ddl is change $(#CountryID)。更改(功能(){ var countryid = parseInt($(#CountryID)。val()); if(!NaN(countryid)){ var ddstate = $(# StateID); ddstate.empty(); ddstate.append($(< option>< / option>)。val() .html(选择状态)); $ .ajax({ url: @ Url.Action(Getstates,user), 类型:GET, 数据:{countryid:countryid}, 数据类型:json, 成功:函数(数据){ $ .each(data,func) (i,val){ ddstate.append( $(< option>< / option>)。val(val.StateID).html (val.StateName) ); }); }, 错误:function(){ 提醒(错误!!!); } }); } }); }) ; < / script> } // //国家/地区列表已填写但状态列表未填写@section Scripts { @Scripts.Render("~/bundles/jqueryval")<script type="text/javascript"> $(document).ready(function () { // this is call wen country ddl is change $("#CountryID").change(function () { var countryid = parseInt($("#CountryID").val()); if (!NaN(countryid)) { var ddstate = $("#StateID"); ddstate.empty(); ddstate.append($("<option></option>").val("").html("select state")); $.ajax({ url:"@Url.Action("Getstates","user")", type: "GET", data: { countryid: countryid }, datatype: "json", success: function (data) { $.each(data, function (i, val) { ddstate.append( $("<option></option>").val(val.StateID).html(val.StateName) ); }); }, error: function () { alert("error!!!"); } }); } }); });</script>}////the country list is fill but state list is not fill推荐答案 (document).ready(function(){ //这是调用wen countr y ddl是变更 (document).ready(function () { // this is call wen country ddl is change (#CountryID)。change(function(){ var countryid = parseInt(("#CountryID").change(function () { var countryid = parseInt( (#CountryID)。val()); if(!NaN(countryid)){ var ddstate = ("#CountryID").val()); if (!NaN(countryid)) { var ddstate = 这篇关于如何在mvc中返回JsonResult的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-29 12:51