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

相关文章

hdu1043(八数码问题,广搜 + hash(实现状态压缩) )

利用康拓展开将一个排列映射成一个自然数,然后就变成了普通的广搜题。 #include<iostream>#include<algorithm>#include<string>#include<stack>#include<queue>#include<map>#include<stdio.h>#include<stdlib.h>#include<ctype.h>#inclu

【C++】_list常用方法解析及模拟实现

相信自己的力量,只要对自己始终保持信心,尽自己最大努力去完成任何事,就算事情最终结果是失败了,努力了也不留遗憾。💓💓💓 目录   ✨说在前面 🍋知识点一:什么是list? •🌰1.list的定义 •🌰2.list的基本特性 •🌰3.常用接口介绍 🍋知识点二:list常用接口 •🌰1.默认成员函数 🔥构造函数(⭐) 🔥析构函数 •🌰2.list对象

【Prometheus】PromQL向量匹配实现不同标签的向量数据进行运算

✨✨ 欢迎大家来到景天科技苑✨✨ 🎈🎈 养成好习惯,先赞后看哦~🎈🎈 🏆 作者简介:景天科技苑 🏆《头衔》:大厂架构师,华为云开发者社区专家博主,阿里云开发者社区专家博主,CSDN全栈领域优质创作者,掘金优秀博主,51CTO博客专家等。 🏆《博客》:Python全栈,前后端开发,小程序开发,人工智能,js逆向,App逆向,网络系统安全,数据分析,Django,fastapi

让树莓派智能语音助手实现定时提醒功能

最初的时候是想直接在rasa 的chatbot上实现,因为rasa本身是带有remindschedule模块的。不过经过一番折腾后,忽然发现,chatbot上实现的定时,语音助手不一定会有响应。因为,我目前语音助手的代码设置了长时间无应答会结束对话,这样一来,chatbot定时提醒的触发就不会被语音助手获悉。那怎么让语音助手也具有定时提醒功能呢? 我最后选择的方法是用threading.Time

Android实现任意版本设置默认的锁屏壁纸和桌面壁纸(两张壁纸可不一致)

客户有些需求需要设置默认壁纸和锁屏壁纸  在默认情况下 这两个壁纸是相同的  如果需要默认的锁屏壁纸和桌面壁纸不一样 需要额外修改 Android13实现 替换默认桌面壁纸: 将图片文件替换frameworks/base/core/res/res/drawable-nodpi/default_wallpaper.*  (注意不能是bmp格式) 替换默认锁屏壁纸: 将图片资源放入vendo

C#实战|大乐透选号器[6]:实现实时显示已选择的红蓝球数量

哈喽,你好啊,我是雷工。 关于大乐透选号器在前面已经记录了5篇笔记,这是第6篇; 接下来实现实时显示当前选中红球数量,蓝球数量; 以下为练习笔记。 01 效果演示 当选择和取消选择红球或蓝球时,在对应的位置显示实时已选择的红球、蓝球的数量; 02 标签名称 分别设置Label标签名称为:lblRedCount、lblBlueCount

poj 1258 Agri-Net(最小生成树模板代码)

感觉用这题来当模板更适合。 题意就是给你邻接矩阵求最小生成树啦。~ prim代码:效率很高。172k...0ms。 #include<stdio.h>#include<algorithm>using namespace std;const int MaxN = 101;const int INF = 0x3f3f3f3f;int g[MaxN][MaxN];int n

Kubernetes PodSecurityPolicy:PSP能实现的5种主要安全策略

Kubernetes PodSecurityPolicy:PSP能实现的5种主要安全策略 1. 特权模式限制2. 宿主机资源隔离3. 用户和组管理4. 权限提升控制5. SELinux配置 💖The Begin💖点点关注,收藏不迷路💖 Kubernetes的PodSecurityPolicy(PSP)是一个关键的安全特性,它在Pod创建之前实施安全策略,确保P

如何在Visual Studio中调试.NET源码

今天偶然在看别人代码时,发现在他的代码里使用了Any判断List<T>是否为空。 我一般的做法是先判断是否为null,再判断Count。 看了一下Count的源码如下: 1 [__DynamicallyInvokable]2 public int Count3 {4 [__DynamicallyInvokable]5 get

2、PF-Net点云补全

2、PF-Net 点云补全 PF-Net论文链接:PF-Net PF-Net (Point Fractal Network for 3D Point Cloud Completion)是一种专门为三维点云补全设计的深度学习模型。点云补全实际上和图片补全是一个逻辑,都是采用GAN模型的思想来进行补全,在图片补全中,将部分像素点删除并且标记,然后卷积特征提取预测、判别器判别,来训练模型,生成的像