MVC dropdownlist二级联动
来源:互联网 发布:泰和安主机编程程序 编辑:程序博客网 时间:2024/05/01 05:20
在做输入的时候,有时候需要用到多个dropdownlist联动,所以在网上搜集了相关资料后,自己也拿具体的MVC案例整理了一下,希望可以帮到也在找这个问题的同学们:
model:
public class namelist { public int index { get; set; } public string sex { get; set; } public string name { get; set; } }
View(View对应的actionresult:Index):
<script type="text/javascript"> $(document).ready(function () { $("#SEX").change(function () { var url = "/practice/GetName/" + $("#SEX").val() + "/"; $.getJSON(url, function (data) { $("#NAME").html(''); $.each(data, function (i, item) { $("#NAME").append($("<option></option>").val(item).html(item)); }) }) }) }) </script> 性别:@Html.DropDownList("SEX", @ViewData["SEX"] as IEnumerable<SelectListItem>,"--Please Choose--") 名字:@Html.DropDownList("NAME", @ViewData["NAME"] as IEnumerable<SelectListItem>,"--Please Choose--")
Controller:
public ActionResult Index() { List<namelist> llNameList = buildNameList(); ViewData["SEX"]=llNameList.Select(i=>new SelectListItem{Value=i.sex,Text=i.sex}); ViewData["NAME"] = llNameList.Select(i => new SelectListItem { Value = i.name, Text = i.name }); return View(); } public JsonResult GetName(string id) //view界面选择sex的,输出对应的name { List<namelist> llNameList = new List<namelist>(); llNameList = buildNameList(); namelist ccnamelist = new namelist(); var varNameList = llNameList.Where(i => i.sex == id).Select(i => i.name); return Json(varNameList.ToList(), JsonRequestBehavior.AllowGet); } public List<namelist> buildNameList() //因未连接数据库,为demo效果,临时建立一个list { List<namelist> llnamelist = new List<namelist>(); for (int _count = 0; _count < 5; _count++) { namelist ccnamelist = new namelist(); ccnamelist.index = _count; ccnamelist.sex = "male"; ccnamelist.name = "Tommy " + _count.ToString(); llnamelist.Add(ccnamelist); } for (int _count = 0; _count < 4; _count++) { namelist ccnamelist = new namelist(); ccnamelist.index = _count; ccnamelist.sex = "female"; ccnamelist.name = "Kitty " + _count.ToString(); llnamelist.Add(ccnamelist); } return llnamelist; }
最终图示:
阅读全文
0 0
- MVC dropdownlist二级联动
- Yii2 二级联动 DropDownList
- C# DropDownList二级联动问题
- Mvc Ajax Dropdownlist 联动实现
- MVC DropDownList 利用Ajax联动
- MVC DropDownList 利用Ajax联动
- MVC里面两个dropdownlist联动
- 实现DropDownList无刷新二级联动
- CheckBoxList 和 DropDownList 的二级联动
- CheckBoxList 和 DropDownList 的二级联动
- DropDownList联动不刷新(二级)
- DropDownList无刷新二级联动(.ashx)
- AJAX实现DropDownList无刷新二级联动
- DropDownList无刷新ajax二级联动效果
- [原创]用SqlDataSource实现DropDownList的二级联动
- C#dropdownlist的二级联动怎么做啊?
- 一个关无DropDownList无刷新二级联动的例子.
- asp.net js实现dropdownlist二级联动(动态)
- Redux-devtools 使用
- 阿里+百度+CVTE面试集
- 提车注意事项清单
- css-如何让你的元素居中?
- 基于CentOS搭建 Git 服务
- MVC dropdownlist二级联动
- 如何跟踪Log4j或Sl4j加载哪个配置文件进行初始化
- python处理3
- 方法调用和数组概念以及for each
- C之有趣-绘制正弦曲线
- Linux下通过ODBC连接数据库
- python 案例 007 (copy list 的两种模式)
- java求数组中最大值最小值
- Java提高篇——equals()与hashCode()方法详解