asp.net MVC3之AJAX实现(json)

        分类:             Asp.net MVC              2011-08-10 13:55     2272人阅读     评论(0)     收藏     举报    
1.建一个mvc3的项目,取名叫MVC3Test
2.修改About.cshtml,如下代码
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>
3.创建几个model
 (1) TestSchool.cs
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; } } }
 (2) TestSchoolDepartment.cs
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; } } }
 (3) TestModels.cs
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; } } }
4.由于About是在Home页面里的,所以它的controller应该在HomeController里,我们添加两个controller,如下:
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)的更多相关文章

  1. 一步步学习ASP.NET MVC3 (10)——@Ajax,JavaScriptResult(1)

    请注明转载地址:http://www.cnblogs.com/arhat 首先老魏先说一下抱歉,昨天由于在安装CentOS,Mono,Jexus配置Linux环境下的ASP.NET运行环境,花费了不少 ...

  2. 一步步学习ASP.NET MVC3 (11)——@Ajax,JavaScriptResult(2)

    请注明转载地址:http://www.cnblogs.com/arhat 今天在补一章吧,由于明天的事可能比较多,老魏可能顾不上了,所以今天就再加把劲在写一章吧.否则对不起大家了,大家看的比较快,可是 ...

  3. asp.net mvc3 利用Ajax实现局部刷新

    1.利用Ajax.ActionLink()方法 首先在_Layout.cshtml文件中加载 运行AJAX必要的Jquery <script src="@Url.Content(&qu ...

  4. 一步步学习ASP.NET MVC3 章节总结

    请注明转载地址:http://www.cnblogs.com/arhat 对于<一步步学习ASP.NET MVC3>系列工15章,那么为了方便大家能够快速的预览,老魏在这里为这个系列提供一 ...

  5. ASP.NET 5 - $.ajax post JSON.stringify(para) is null

    JavaScript 代码: var para = {}; para.id = $("#ad-text-id").val(); para.title = $("#ad-t ...

  6. ASP.NET MVC3入门教程之ajax交互

    本文转载自:http://www.youarebug.com/forum.php?mod=viewthread&tid=100&extra=page%3D1 随着web技术的不断发展与 ...

  7. asp.net的ajax以及json

    asp.net的ajax以及json 来现在这家公司以前,从未接触过webform,以前在学校做的项目是php,java以及asp.net mvc的,当时asp.net mvc用的是razor引擎,所 ...

  8. 如何构建ASP.NET MVC4&JQuery&AJax&JSon示例

    背景: 博客中将构建一个小示例,用于演示在ASP.NET MVC4项目中,如何使用JQuery Ajax. 步骤: 1,添加控制器(HomeController)和动作方法(Index),并为Inde ...

  9. 在ASP.NET MVC3 中利用Jsonp跨域访问

    在ASP.NET MVC3 中利用Jsonp跨域访问 在信息系统开发的时,根据相关业务逻辑难免会多系统之间互相登录.一般情况下我们需要在多系统之间使用多个用户名和密码.这样客户就需要在多个系统之间重复 ...

随机推荐

  1. 条件编译,C语言条件编译详解

    条件编译是指预处理器根据条件编译指令,有条件地选择源程序代码中的一部分代码作为输出,送给编译器进行编译.主要是为了有选择性地执行相应操作,防止宏替换内容(如文件等)的重复包含.常见的条件编译指令如表 ...

  2. bzoj 4182

    首先很容易看出这是一个树上多重背包问题 设状态$f[i][j]$表示以$i$为根的子树中利用的体积是$j$ 但是题目中有要求:选择的点集必须是一个联通块 这要怎么处理? 点分治! 首先我们利用点分治的 ...

  3. 【大数据系统架构师】0.1 Java编程基础

    1. 初识Java 2. Java语法 快速入门点我 2.1 数据类型和运算符 2.2 流程控制语句 2.3 数组 2.4 类和对象 2.5 OOP三大特性 2.6 集合框架与泛型 2.7 反射机制 ...

  4. webservice怎么给对方提供报文,即wsdl文件

    1.webservice发布后在网页打开服务,点击服务说明 2.打开这样一个页面,ctrl+s保存网页,后缀改为wsdl,搞定

  5. PLSQL导出语句的查询结果

    不需要把全部结果都展示出来

  6. 【bzoj5093】 [Lydsy1711月赛]图的价值 组合数+斯特林数+NTT

    Description "简单无向图"是指无重边.无自环的无向图(不一定连通). 一个带标号的图的价值定义为每个点度数的k次方的和. 给定n和k,请计算所有n个点的带标号的简单无向 ...

  7. 题解 CF948A 【Protect Sheep】

    题目链接 额..这道题亮点在: $you$ $do$ $not$ $need$ $to$ $minimize$ $their$ $number.$ 所以说嘛... 直接判断狼的四周有没有紧挨着的羊,没 ...

  8. 详细解读Python中的__init__()方法

    init()方法意义重大的原因有两个.第一个原因是在对象生命周期中初始化是最重要的一步:每个对象必须正确初始化后才能正常工作.第二个原因是init()参数值可以有多种形式. 因为有很多种方式为init ...

  9. (Keil) Debug & Simulation 操作

    0x00 printf在MCU環境下print debug error message,利用Logic Analyzer模擬MCU register or GPIO狀態. 若是要要使用printf函數 ...

  10. @ApiParam @PathVariable @RequestParam三者区别

    转载:https://www.cnblogs.com/xu-lei/p/7803062.html @ApiParam @PathVariable @RequestParam三者区别 1.@ApiPar ...