ASP.NET MVC DropDownList的使用
来源:互联网 发布:苹果6网络通信出现问题 编辑:程序博客网 时间:2024/05/21 22:27
一、控制器·
public ActionResult Index() { if (ViewBag.courseID != null) { } else { ViewBag.studentID = new SelectList(db.Students,"ID","LastName"); ViewBag.courseID = new SelectList(db.Courses, "CourseID", "Title"); } return View(); }
二、Ajax
(1)服务器
public ActionResult DDL(int studentID) { //string a = studentID; //var courseID = db.Enrollments.Where(s => s.StudentID == studentID).Select(s=>s.CourseID); //foreach (var cid in courseID) //{ // var course = db.Courses.Where(s => s.CourseID == cid); // IEnumerable<> //} //var q = from a in db.Enrollments // join b in db.Students on a.StudentID equals studentID // join c in db.Courses on a.CourseID equals c.CourseID // group new { // c=c // } by c.CourseID into g // select new // { // CourseID = g.Key, // Title = g.FirstOrDefault, // }; var q = from a in db.Enrollments from b in db.Students from c in db.Courses where a.StudentID == studentID && a.CourseID == c.CourseID select new { CourseID = c.CourseID, Title = c.Title }; var result=q.Distinct().ToList();//去重Distinct() //return RedirectToAction("Index"); return Json(q.Distinct().ToList()); }(2)前端
@using (Html.BeginForm("DDL", "Home")) { @Html.Label("课程:") @*@Html.DropDownList("courseID")*@ @Html.DropDownList("studentID") @Html.DropDownList("courseID",ViewBag.Course as IEnumerable<SelectListItem>, "请选择课程") <input type="submit"value="提交" /> }
<script type="text/javascript"> //$(function () { // $("#courseID").change(function () { // alert(this.id) // }) //}) //$(function () { // $("#courseID").change(function () { // alert($("#courseID").val()) // //alert(this.val()) // }) //}) //var value=$("#courseID").val(); //alert(value); $(function () { $("#studentID").change(function () { $.ajax({ url: "@Url.Action("DDL","Home")" + "?studentID= " + $(this).val(), type: 'POST', data: null, dataType: "json", success: function (data) { $("#courseID").empty(); // $("#courseID").append("<option value=0>Ajax</option>") var json = eval(data); //数组 $.each(json, function (index, item) { //循环获取数据 var CourseID = json[index].CourseID; var Title = json[index].Title; // $("#list").html($("#list").html() + "<br>" + name + " - " + idnumber + " - " + sex + "<br/>"); $("<option value='" + CourseID + "'>" + Title + "</option>").appendTo("#courseID"); }); } }); }); }); </script>@Html.DropDownList("studentID"),studentID即为ViewBag.studentID,studentID也是下拉框的id,下拉框的Value和Text会自动填充。
@Html.DropDownList("courseID",ViewBag.Course as IEnumerable<SelectListItem>, "请选择课程"),第一个参数是下拉框的id,第二个即ViewBag.Course,第三个参数为下拉框Text默认值。
Linq To SQL三张表联结查询,Distinct()去重。
JQuery Ajax语法,使用Ajax实现DropDownList两级联动,在ASP.NET MVC中Ajax最好使用POST方式,否则会报500的错误码。
阅读全文
0 0
- ASP.NET MVC中DROPDOWNLIST的使用
- ASP.NET MVC中DropDownList的使用
- asp.net mvc Dropdownlist的使用绑定
- ASP.NET MVC DropDownList的使用
- ASP.NET MVC DropdownList 使用
- 详解ASP.NET MVC中DropDownList的使用
- 详解ASP.NET MVC中DropDownList的使用
- 在ASP.NET MVC中使用DropDownList
- 在ASP.NET MVC中使用DropDownList
- 在ASP.NET MVC中使用DropDownList
- 在ASP.NET MVC中使用DropDownList
- asp.net mvc dropdownlist
- Html.DropDownList()的用法 ( Asp.Net MVC)
- Html.DropDownList()的用法 ( Asp.Net MVC)
- Html.DropDownList()的用法 ( Asp.Net MVC)
- NET MVC DropDownList使用
- ASP.NET MVC DropDownList 用法
- asp.net的DropDownList控件的使用
- SSM三大框架整合配置(2)-----Spring+Mybatis
- Multithreading in Python
- 07 C++模板特化、模板偏特化和模板模板参数基本用法(学自Boolean)
- vue.js_v-model笔记
- async/await
- ASP.NET MVC DropDownList的使用
- cenos系统python项目报错mysql Access denied for user root@localhost错误解决方法
- 深夜博客
- C++标准库中string类型与数值类型之间的转换
- Spring声明式事务注意点,以及不生效情况
- 小游戏--三子棋(任意几子棋)
- async/await
- System.arraycopy()和Arrays.copyOf()的区别
- ActiveMQ(1)-初探