MVC5中后台提供Json,前台处理Json,绑定给Dropdownlist的例子
来源:互联网 发布:烟台淘宝运营 编辑:程序博客网 时间:2024/05/18 18:01
MVC5中后台提供Json,前台处理Json,绑定给Dropdownlist的例子:
前端:
我们以前在前端定义了两个控件:
<div class="row"> <div class="col-md-1">国家</div> <div class="col-md-11"> @Html.DropDownListFor(model => model.Director.HometownID, MvcEntitiyFrameWork.ViewModels.HometownViewModel.GetHometownSelectList(), htmlAttributes: new { @class = "form-control", @style = "width:120px;" }) </div> </div> <div class="row" style="margin-bottom:2px"> <div class="col-md-1">导演</div> <div class="col-md-11"> @Html.DropDownListFor(model => model.DirectorID, MvcEntitiyFrameWork.ViewModels.DirectorViewModel.GetDirectorSelectList(Model.Director.HometownID.ToString()), htmlAttributes: new { @class = "form-control", @style = "width:120px;" }) @Html.ValidationMessageFor(model => model.DirectorID, "", new { @class = "text-danger" }) </div> </div>
然后我们在脚本中执行如下代码:
特别的一点,我们在上面代码中貌似没指定控件的ID,但是运行以后在页面是可以看到控件的ID的,从而,我从运行后的界面得到ID来使用。
<script type="text/javascript"> $("#Director_HometownID").change( function () { GetDirector($(this).val()); } ); function GetDirector(message) { var option = { url: '/Movies/GetDirectorList', type: 'get', chche: false, dataType: 'json', data: { _htid: message }, //发送服务器数据 success: function (data) { //成功事件 $("#DirectorID").empty(); $.each(data, function (i, item) { $("<option></option>").val(data[i].Value).text(data[i].Text).appendTo($("#DirectorID")); }); }, error: function (XMLHttpRequest, textStatus, errorThrown) { //发送失败事件 alert(textStatus ); } }; //进行异步传输 $.ajax(option); } </script>
我们的后台,controller中定义如下,返回ActionResult
public ActionResult GetDirectorList(string _htid) { try { //方法一 Ok //var selectList = db.Directors.Where(a => a.HometownID.ToString() == _htid).Select(a => new SelectListItem //{ // Text = a.DirectorName, // Value = a.DirectorID.ToString() //}); //return Json(selectList, JsonRequestBehavior.AllowGet); //方法二 也Ok List<Director> list = db.Database.SqlQuery<Director>("select DirectorID,DirectorName,HometownID from Directors where HometownID='" + _htid + "'").ToList(); SelectList pList = new SelectList(list, "DirectorID", "DirectorName"); return Json(pList, JsonRequestBehavior.AllowGet); } catch (Exception ex) { return null; } }
前端就可以得到返回值,对dropdownlistbox数据进行绑定了(这里是dropdownlistbox级联筛选数据的应用)
0 0
- MVC5中后台提供Json,前台处理Json,绑定给Dropdownlist的例子
- 后台和前台对json的处理
- 用json形式后台返回list前台处理例子
- 后台传递给前台,Json获取的值为undefined
- java 后台返回json数据给前台
- java 后台返回json数据给前台
- 关于后台返回json数据给前台
- 关于前台js中json的处理
- json的处理java后台和前台之间
- EXT 中json格式数据后台与前台的交互
- Django中前台传json格式后台
- 在隐藏域使用json的注意事项(后台-前台-javascript)+javascript处理json串
- 前台后台的的json数据交互
- 前台调用后台返回json的事例
- JSON前台与后台的交互
- 前台接收后台传的json,json中value的值为一个数组
- springmvc后台如何给前台传json数据
- jquery处理后台返回来的JSON,JSON中属性为JSON,或者数组的处理
- poj--1141 Brackets Sequence(dp)
- 实习入职第二天:ant学习(1)
- php创建软链接
- 【LeetCode-106】Construct Binary Tree from Inorder and Postorder Traversal
- 程序员这个职业的危险期你知道吗
- MVC5中后台提供Json,前台处理Json,绑定给Dropdownlist的例子
- cocopods升级到1.0,导入第三方库,出现问题及解决方式
- 源码提供!Android即时通讯和sns开源项目汇总
- CocoaPods的最新安装流程
- BZOJ 4521: [Cqoi2016]手机号码
- caffe学习笔记(1):数据层定义
- UITableView 自定义cell
- 拦截器 过滤器 配置
- IOS之GCD详细步骤