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

相关文章

Python获取中国节假日数据记录入JSON文件

《Python获取中国节假日数据记录入JSON文件》项目系统内置的日历应用为了提升用户体验,特别设置了在调休日期显示“休”的UI图标功能,那么问题是这些调休数据从哪里来呢?我尝试一种更为智能的方法:P... 目录节假日数据获取存入jsON文件节假日数据读取封装完整代码项目系统内置的日历应用为了提升用户体验,

SpringBoot3实现Gzip压缩优化的技术指南

《SpringBoot3实现Gzip压缩优化的技术指南》随着Web应用的用户量和数据量增加,网络带宽和页面加载速度逐渐成为瓶颈,为了减少数据传输量,提高用户体验,我们可以使用Gzip压缩HTTP响应,... 目录1、简述2、配置2.1 添加依赖2.2 配置 Gzip 压缩3、服务端应用4、前端应用4.1 N

SpringBoot实现数据库读写分离的3种方法小结

《SpringBoot实现数据库读写分离的3种方法小结》为了提高系统的读写性能和可用性,读写分离是一种经典的数据库架构模式,在SpringBoot应用中,有多种方式可以实现数据库读写分离,本文将介绍三... 目录一、数据库读写分离概述二、方案一:基于AbstractRoutingDataSource实现动态

Python FastAPI+Celery+RabbitMQ实现分布式图片水印处理系统

《PythonFastAPI+Celery+RabbitMQ实现分布式图片水印处理系统》这篇文章主要为大家详细介绍了PythonFastAPI如何结合Celery以及RabbitMQ实现简单的分布式... 实现思路FastAPI 服务器Celery 任务队列RabbitMQ 作为消息代理定时任务处理完整

使用Jackson进行JSON生成与解析的新手指南

《使用Jackson进行JSON生成与解析的新手指南》这篇文章主要为大家详细介绍了如何使用Jackson进行JSON生成与解析处理,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1. 核心依赖2. 基础用法2.1 对象转 jsON(序列化)2.2 JSON 转对象(反序列化)3.

Java枚举类实现Key-Value映射的多种实现方式

《Java枚举类实现Key-Value映射的多种实现方式》在Java开发中,枚举(Enum)是一种特殊的类,本文将详细介绍Java枚举类实现key-value映射的多种方式,有需要的小伙伴可以根据需要... 目录前言一、基础实现方式1.1 为枚举添加属性和构造方法二、http://www.cppcns.co

使用Python实现快速搭建本地HTTP服务器

《使用Python实现快速搭建本地HTTP服务器》:本文主要介绍如何使用Python快速搭建本地HTTP服务器,轻松实现一键HTTP文件共享,同时结合二维码技术,让访问更简单,感兴趣的小伙伴可以了... 目录1. 概述2. 快速搭建 HTTP 文件共享服务2.1 核心思路2.2 代码实现2.3 代码解读3.

MySQL双主搭建+keepalived高可用的实现

《MySQL双主搭建+keepalived高可用的实现》本文主要介绍了MySQL双主搭建+keepalived高可用的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,... 目录一、测试环境准备二、主从搭建1.创建复制用户2.创建复制关系3.开启复制,确认复制是否成功4.同

Java实现文件图片的预览和下载功能

《Java实现文件图片的预览和下载功能》这篇文章主要为大家详细介绍了如何使用Java实现文件图片的预览和下载功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... Java实现文件(图片)的预览和下载 @ApiOperation("访问文件") @GetMapping("

使用Sentinel自定义返回和实现区分来源方式

《使用Sentinel自定义返回和实现区分来源方式》:本文主要介绍使用Sentinel自定义返回和实现区分来源方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Sentinel自定义返回和实现区分来源1. 自定义错误返回2. 实现区分来源总结Sentinel自定