asp.net MVC3之AJAX实现(json)
asp.net MVC3之AJAX实现(json)
About.cshtml
About.cshtml
@{ ViewBag.Title = "About Us"; } <script type="text/javascript">
$(function () { //get the schools $.get("/Home/GetSchools", function (data) { $(data).each(function () { var o = document.createElement("option"); o.value =this['Id']; o.text =this['Name']; $("#sltSchool")[].options.add(o); }); }); // Get the departments depend on the school $("#sltSchool").change(function () { // initialization the select $("#sltDepartment").empty(); var _o = document.createElement("option"); _o.value ="-1"; _o.text ="select..."; $("#sltDepartment")[].options.add(_o); $.get("/Home/GetDepartments", { schoolId: $("#sltSchool").val() }, function (data) { $(data).each(function () { var o = document.createElement("option"); o.value =this['Id']; o.text =this['Name']; $("#sltDepartment")[].options.add(o); }); }); }); });</script><div><h2> About</h2><p> Put content here. </p><div><span><label> School : </label><select id="sltSchool"><option value="-1">select...</option></select></span><span style="margin-left: 50px"><label> Department :</label><select id="sltDepartment"><option value="-1">select...</option></select></span></div></div>
TestSchool
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace MVC3Test.Models { publicclass TestSchool { publicint Id { get; set; } publicstring Name { get; set; } } }
TestSchoolDepartment.cs
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace MVC3Test.Models { publicclass TestSchoolDepartment { publicint Id { get; set; } publicint SchoolId { get; set; } publicstring Name { get; set; } } }
TestModels.cs
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace MVC3Test.Models { publicclass TestModels { publicstatic List<TestSchool> GetAllSchools() { returnnew List<TestSchool>() { new TestSchool{Id=,Name="ABC"}, new TestSchool{Id=,Name="DEF"}, new TestSchool{Id=,Name="HIJ"}, new TestSchool{Id=,Name="LMN"} }; } publicstatic List<TestSchoolDepartment> GetAllDepartment() { returnnew List<TestSchoolDepartment>() { new TestSchoolDepartment{Id=,SchoolId=,Name="ABC_D1"}, new TestSchoolDepartment{Id=,SchoolId=,Name="ABC_D2"}, new TestSchoolDepartment{Id=,SchoolId=,Name="ABC_D3"}, new TestSchoolDepartment{Id=,SchoolId=,Name="DEF_D1"}, new TestSchoolDepartment{Id=,SchoolId=,Name="DEF_D2"}, new TestSchoolDepartment{Id=,SchoolId=,Name="HIJ_D1"}, new TestSchoolDepartment{Id=,SchoolId=,Name="HIJ_D2"}, new TestSchoolDepartment{Id=,SchoolId=,Name="HIJ_D3"}, new TestSchoolDepartment{Id=,SchoolId=,Name="HIJ_D4"}, new TestSchoolDepartment{Id=,SchoolId=,Name="LMN_D1"} }; } publicstatic List<TestSchoolDepartment> GetDepartmentBySchoolId(int schoolId) { List<TestSchoolDepartment> testSchoolDepartment =new List<TestSchoolDepartment>(); foreach (TestSchoolDepartment department in GetAllDepartment()) { if (department.SchoolId == schoolId) { testSchoolDepartment.Add(department); } } return testSchoolDepartment; } } }
HomeController.cs
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using MVC3Test.Models; using System.Text; namespace MVC3Test.Controllers { publicclass HomeController : Controller { public ActionResult Index() { ViewBag.Message ="Welcome to ASP.NET MVC!"; return View(); } public ActionResult About() { return View(); }
public JsonResult GetSchools() { returnJson(TestModels.GetAllSchools (),JsonRequestBehavior.AllowGet); } public JsonResult GetDepartments(int schoolId) { returnJson(TestModels.GetDepartmentBySchoolId(schoolId),JsonRequestBehavior.AllowGet); }
}}
asp.net MVC3之AJAX实现(json)的更多相关文章
- 一步步学习ASP.NET MVC3 (10)——@Ajax,JavaScriptResult(1)
请注明转载地址:http://www.cnblogs.com/arhat 首先老魏先说一下抱歉,昨天由于在安装CentOS,Mono,Jexus配置Linux环境下的ASP.NET运行环境,花费了不少 ...
- 一步步学习ASP.NET MVC3 (11)——@Ajax,JavaScriptResult(2)
请注明转载地址:http://www.cnblogs.com/arhat 今天在补一章吧,由于明天的事可能比较多,老魏可能顾不上了,所以今天就再加把劲在写一章吧.否则对不起大家了,大家看的比较快,可是 ...
- asp.net mvc3 利用Ajax实现局部刷新
1.利用Ajax.ActionLink()方法 首先在_Layout.cshtml文件中加载 运行AJAX必要的Jquery <script src="@Url.Content(&qu ...
- 一步步学习ASP.NET MVC3 章节总结
请注明转载地址:http://www.cnblogs.com/arhat 对于<一步步学习ASP.NET MVC3>系列工15章,那么为了方便大家能够快速的预览,老魏在这里为这个系列提供一 ...
- ASP.NET 5 - $.ajax post JSON.stringify(para) is null
JavaScript 代码: var para = {}; para.id = $("#ad-text-id").val(); para.title = $("#ad-t ...
- ASP.NET MVC3入门教程之ajax交互
本文转载自:http://www.youarebug.com/forum.php?mod=viewthread&tid=100&extra=page%3D1 随着web技术的不断发展与 ...
- asp.net的ajax以及json
asp.net的ajax以及json 来现在这家公司以前,从未接触过webform,以前在学校做的项目是php,java以及asp.net mvc的,当时asp.net mvc用的是razor引擎,所 ...
- 如何构建ASP.NET MVC4&JQuery&AJax&JSon示例
背景: 博客中将构建一个小示例,用于演示在ASP.NET MVC4项目中,如何使用JQuery Ajax. 步骤: 1,添加控制器(HomeController)和动作方法(Index),并为Inde ...
- 在ASP.NET MVC3 中利用Jsonp跨域访问
在ASP.NET MVC3 中利用Jsonp跨域访问 在信息系统开发的时,根据相关业务逻辑难免会多系统之间互相登录.一般情况下我们需要在多系统之间使用多个用户名和密码.这样客户就需要在多个系统之间重复 ...
随机推荐
- 关于MultiDataTrigger和MultiTrigger的一些注意事项
他俩有着相同的语法. 都是在conditions中编写触发条件. 因为都是同一个触发类. 在conditions中有Property和Binding这两个属性.那么这两个可以同时使用吗?当然是不可以的 ...
- C语言宏的定义和宏的使用方法(#define)
1.宏的功能介绍 在 C 语言中,可以采用命令 #define 来定义宏.该命令允许把一个名称指定成任何所需的文本,例如一个常量值或者一条语句.在定义了宏之后,无论宏名称出现在源代码的何处,预处理器都 ...
- 解决Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker fro问题
项目中碰到一个问题,就是将一个map转换成json格式的时候出现错误,最后排查将延迟加载关闭后成功转换,因为数据量较大,于是重新创建了一个对象进行接收. 解决办法是在配置文件中进行配置 虽然解决了这个 ...
- 设置使用的python版本
一.查看当前使用的python版本,或设置使用的python版本 二.python2中默认使用ASCII码,无法识别中文,报错如图,解决办法,设置字符集为utf-8
- 老男孩Day15作业:商城列表页面(静态)
一. 一.作业需求: 1.完成商城列表静态页面的抒写 二.博客地址:https://www.cnblogs.com/catepython/p/9205636.html 三.运行环境 操作系统:Win1 ...
- P4313 文理分科 最小割
$ \color{#0066ff}{ 题目描述 }$ 文理分科是一件很纠结的事情!(虽然看到这个题目的人肯定都没有纠结过) 小P所在的班级要进行文理分科.他的班级可以用一个n*m的矩阵进行描述,每个格 ...
- 【离散数学】SDUT OJ 指定长度路径数
指定长度路径数 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Problem Description 题目给出一个有n个节点 ...
- hdp 2.06 安装备忘
1,官方安装说明文档 http://docs.hortonworks.com/HDPDocuments/HDP2/HDP-2.0.6.0-Win/bk_installing_hdp_for_windo ...
- Android Activity实例应用(选择QQ头像)
1.效果图 点击button,跳转到页面2 选择需要的头像,自动返回 3.XML文件布局 页面1 <?xml version="1.0" encoding="utf ...
- Kibana6.x.x源码分析--如何使用kibana的savedObjectType对象
默认kibana插件定义了三种保存实体对象[savedObjectType],如下图所示: 要使用只需要在自己定义的app的uses属性中添加上:savedObjectTypes 即可,如下图所示: ...