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. jmeter函数助手(_random、_time)

    jmeter函数助手 __random __time yyyyMMddHHmmdd  时间格式(年月日时分秒) 1.打开函数助手对话框,选项->函数助手对话框 2.生成函数字符串 (1)选择功能 ...

  2. Python实现——二次多项式回归(最小二乘法)

    2019/3/25 真的,当那个图像出现的时候,我真的感觉太美了. 或许是一路上以来自我的摸索加深的我对于这个模型的感受吧. 二次函数拟合--最小二乘法公式法 与线性回归相似,对二次函数进行拟合某种意 ...

  3. 如果你是后台开发者,InnoDB的小秘密,你应该知道

    InnoDB简述 InnoDB由Innobase Oy公司所开发,2006年五月时由甲骨文公司并购.InnoDB支持了ACID兼容的事务(Transaction)功能,类似于PostgreSQL. 当 ...

  4. SDUT OJ 顺序表应用3:元素位置互换之移位算法

    顺序表应用3:元素位置互换之移位算法 Time Limit: 1000 ms Memory Limit: 570 KiB Submit Statistic Discuss Problem Descri ...

  5. count distinct 组合使用

    SELECT COUNT(DISTINCT Lbox_Sn) FROM Tab_History_Info

  6. 3、pandas

    原文出处: pandas.pydata.org   译文出处:石卓林 这是关于pandas的简短介绍,主要面向新用户.可以参阅Cookbook了解更复杂的使用方法. 链接:http://python. ...

  7. CBoard 看板参数管理

    看板设计采用简单Row+Column布局模式,每行总长度为12,每列对应一个图表,行高度可以调节,列高度集成行高 左边栏看板分类中,我的看板为当前用户创建的看板,普通看板分类通过分类管理维护,保存看板 ...

  8. 洛谷 P3376【模板】网络最大流

    题目描述 如题,给出一个网络图,以及其源点和汇点,求出其网络最大流. 输入输出格式 输入格式: 第一行包含四个正整数N.M.S.T,分别表示点的个数.有向边的个数.源点序号.汇点序号. 接下来M行每行 ...

  9. LightOJ - 1140 统计0的数位 数位DP

    注意以下几点: 搜索维度非约束条件的都要记录,否则大概率出错,比如_0 st参数传递和_0的互相影响要分辨清楚 num==-1就要返回0而不是1 #include<iostream> #i ...

  10. python 函数的递归

    递归:简单来说就是自己调用自己 这里我们又要举个例子来说明递归能做的事情. 例一: 现在你们问我,alex老师多大了?我说我不告诉你,但alex比 egon 大两岁. 你想知道alex多大,你是不是还 ...