asp.net MVC3之AJAX实现(json)

2024-02-15 13:08
文章标签 实现 json asp net ajax mvc3

本文主要是介绍asp.net MVC3之AJAX实现(json),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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")[0].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")[0].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")[0].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 {public class TestSchool{public int Id { get; set; }public string Name { get; set; }} }
(2) TestSchoolDepartment.cs
TestSchoolDepartment.cs
    
using System; using System.Collections.Generic; using System.Linq; using System.Web;namespace MVC3Test.Models {public class TestSchoolDepartment{public int Id { get; set; }public int SchoolId { get; set; }public string Name { get; set; }} }
(3) TestModels.cs
TestModels.cs
    
using System; using System.Collections.Generic; using System.Linq; using System.Web;namespace MVC3Test.Models {public class TestModels{public static List<TestSchool> GetAllSchools(){return new List<TestSchool>(){new TestSchool{Id=1,Name="ABC"},new TestSchool{Id=2,Name="DEF"},new TestSchool{Id=3,Name="HIJ"},new TestSchool{Id=4,Name="LMN"}};}public static List<TestSchoolDepartment> GetAllDepartment(){return new List<TestSchoolDepartment>(){new TestSchoolDepartment{Id=1,SchoolId=1,Name="ABC_D1"},new TestSchoolDepartment{Id=2,SchoolId=1,Name="ABC_D2"},new TestSchoolDepartment{Id=3,SchoolId=1,Name="ABC_D3"},new TestSchoolDepartment{Id=4,SchoolId=2,Name="DEF_D1"},new TestSchoolDepartment{Id=5,SchoolId=2,Name="DEF_D2"},new TestSchoolDepartment{Id=6,SchoolId=3,Name="HIJ_D1"},new TestSchoolDepartment{Id=7,SchoolId=3,Name="HIJ_D2"},new TestSchoolDepartment{Id=8,SchoolId=3,Name="HIJ_D3"},new TestSchoolDepartment{Id=9,SchoolId=3,Name="HIJ_D4"},new TestSchoolDepartment{Id=10,SchoolId=4,Name="LMN_D1"}};}public static 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 {public class HomeController : Controller{public ActionResult Index(){ViewBag.Message = "Welcome to ASP.NET MVC!";return View();}public ActionResult About(){return View();}
       
public JsonResult GetSchools(){return Json(TestModels.GetAllSchools (),JsonRequestBehavior.AllowGet);}public JsonResult GetDepartments(int schoolId){return Json(TestModels.GetDepartmentBySchoolId(schoolId),JsonRequestBehavior.AllowGet);}
}}
好了,所有的代码都已完成,现在只要编译、运行项目即可。

这篇关于asp.net MVC3之AJAX实现(json)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/711474

相关文章

Java对象和JSON字符串之间的转换方法(全网最清晰)

《Java对象和JSON字符串之间的转换方法(全网最清晰)》:本文主要介绍如何在Java中使用Jackson库将对象转换为JSON字符串,并提供了一个简单的工具类示例,该工具类支持基本的转换功能,... 目录前言1. 引入 Jackson 依赖2. 创建 jsON 工具类3. 使用示例转换 Java 对象为

Vue项目的甘特图组件之dhtmlx-gantt使用教程和实现效果展示(推荐)

《Vue项目的甘特图组件之dhtmlx-gantt使用教程和实现效果展示(推荐)》文章介绍了如何使用dhtmlx-gantt组件来实现公司的甘特图需求,并提供了一个简单的Vue组件示例,文章还分享了一... 目录一、首先 npm 安装插件二、创建一个vue组件三、业务页面内 引用自定义组件:四、dhtmlx

Vue ElementUI中Upload组件批量上传的实现代码

《VueElementUI中Upload组件批量上传的实现代码》ElementUI中Upload组件批量上传通过获取upload组件的DOM、文件、上传地址和数据,封装uploadFiles方法,使... ElementUI中Upload组件如何批量上传首先就是upload组件 <el-upl

Node.js net模块的使用示例

《Node.jsnet模块的使用示例》本文主要介绍了Node.jsnet模块的使用示例,net模块支持TCP通信,处理TCP连接和数据传输,具有一定的参考价值,感兴趣的可以了解一下... 目录简介引入 net 模块核心概念TCP (传输控制协议)Socket服务器TCP 服务器创建基本服务器服务器配置选项服

Docker部署Jenkins持续集成(CI)工具的实现

《Docker部署Jenkins持续集成(CI)工具的实现》Jenkins是一个流行的开源自动化工具,广泛应用于持续集成(CI)和持续交付(CD)的环境中,本文介绍了使用Docker部署Jenkins... 目录前言一、准备工作二、设置变量和目录结构三、配置 docker 权限和网络四、启动 Jenkins

Python3脚本实现Excel与TXT的智能转换

《Python3脚本实现Excel与TXT的智能转换》在数据处理的日常工作中,我们经常需要将Excel中的结构化数据转换为其他格式,本文将使用Python3实现Excel与TXT的智能转换,需要的可以... 目录场景应用:为什么需要这种转换技术解析:代码实现详解核心代码展示改进点说明实战演练:从Excel到

如何使用CSS3实现波浪式图片墙

《如何使用CSS3实现波浪式图片墙》:本文主要介绍了如何使用CSS3的transform属性和动画技巧实现波浪式图片墙,通过设置图片的垂直偏移量,并使用动画使其周期性地改变位置,可以创建出动态且具有波浪效果的图片墙,同时,还强调了响应式设计的重要性,以确保图片墙在不同设备上都能良好显示,详细内容请阅读本文,希望能对你有所帮助...

C# string转unicode字符的实现

《C#string转unicode字符的实现》本文主要介绍了C#string转unicode字符的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随... 目录1. 获取字符串中每个字符的 Unicode 值示例代码:输出:2. 将 Unicode 值格式化

python安装whl包并解决依赖关系的实现

《python安装whl包并解决依赖关系的实现》本文主要介绍了python安装whl包并解决依赖关系的实现,文中通过图文示例介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面... 目录一、什么是whl文件?二、我们为什么需要使用whl文件来安装python库?三、我们应该去哪儿下

Python脚本实现图片文件批量命名

《Python脚本实现图片文件批量命名》这篇文章主要为大家详细介绍了一个用python第三方库pillow写的批量处理图片命名的脚本,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下... 目录前言源码批量处理图片尺寸脚本源码GUI界面源码打包成.exe可执行文件前言本文介绍一个用python第三方库pi