1.建一个mvc3的项目,取名叫MVC3Test(事先安装mvc3 version:1.13.113.0)
2.修改About.cshtml,如下代码
About.cshtml
@{
ViewBag.Title = "About Us";
}
<script type="text/javascript">
$(function () {
$.ajax({
url: "/Home/GetSchools",
type: "POST",
contentType: "application/json; charset=utf-8",
data: "{}",
dataType: "json",
success: function (data) {
$("#sltSchool").empty();
$("#sltSchool").html(data);
},
error: function ErrorCallback(XMLHttpRequest, textStatus, errorThrown) {
alert(errorThrown + ":" + textStatus);
}
});
// Get the departments depend on the school
$("#sltSchool").change(function () {
GetDepartments($("#sltSchool").val());
});
});
function GetDepartments(sId) {
$.ajax({
url: "/Home/GetSecondCatalog",
type: "POST",
contentType: "application/json; charset=utf-8",
data: "{schoolId:"+sId+"}",
dataType: "json",
success: function (data) {
$('#sltDepartment').empty();
$('#sltDepartment').html(data);
},
error: function ErrorCallback(XMLHttpRequest, textStatus, errorThrown) {
alert(errorThrown + ":" + textStatus);
}
});
}
</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,如下:
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();
}
[HttpPost]
public JsonResult GetSchools()
{
StringBuilder sb = new StringBuilder();
sb.Append("<option value=\"-1\">select...</option>");
foreach (var item in TestModels.GetAllSchools())
{
sb.AppendFormat("<option value=\"{0}\">{1}</option>", item.Id, item.Name);
}
string result = sb.ToString();
return this.Json(result, JsonRequestBehavior.AllowGet);
}
[HttpPost]
public JsonResult GetSecondCatalog(int schoolId)
{
StringBuilder sb = new StringBuilder();
sb.Append("<option value=\"-1\">select...</option>");
foreach (var item in TestModels.GetDepartmentBySchoolId(schoolId))
{
sb.AppendFormat("<option value=\"{0}\">{1}</option>", item.Id, item.Name);
}
string result = sb.ToString();
return this.Json(result, JsonRequestBehavior.AllowGet);
}
}
}
分享到:
相关推荐
[MVC]bootstrap-table表格ajax获取json数据并分页。
本例子用到Spring MVC , Jdbc, JSon, Gson 等等技术,Spring Servlet 配置,Spring中ClassPathXmlApplicationContext类的简单使用,Jdbc的配置与数据库的读取,List与json的转换,java对象使⽤Gson转json字符串null...
前台Ajax请求很多时候需要从后台获取JSON格式数据,一般有以下方式: 拼接字符串 return Content({\id\:\1\,\name\:\A\}); 为了严格符合Json数据格式,对双引号进行了转义。 使用JavaScriptSerialize.Serialize()...
3.Spring MVC 请求参数的获取案例 4.Spring MVC 域对象共享数据案例 5.Spring MVC @ModelAttribute 注解案例 6.Spring MVC 国际化案例 7.Spring MVC 请求转发与请求重定向案例 8.Spring MVC 下载 Excel 文档的需求...
在本示例中,我们将向您展示如何将对象转换成json格式并通过spring mvc框架返回给用户。 使用技术及环境: Spring 3.2.2.RELEASE Jackson 1.9.10 JDK 1.6 Eclipse 3.6 Maven 3 PS:在spring 3 中,要输出json...
1.1版本更新 * 新增系统设置功能 * MyORM新增获取DataTable函数 * Map新增getString()函数 * 自动判断浏览器是否支持gzip压缩,返回压缩后JSON easy-mvc框架特点: * 摒弃微软庞大的mvc库,只有1个dll * 自动扫描...
* MyORM新增获取DataTable函数 * Map新增getString()函数 * 自动判断浏览器是否支持gzip压缩,返回压缩后JSON easy-mvc框架特点: * 摒弃微软庞大的mvc库,只有1个dll * 自动扫描DAL接口并实现注册,无需指定...
jsp+servlet+ajax使用json作为数据...2.根据姓名获取该对象使用gson将对象转换成json后返回给客户端并显示,完成修改功能时经常使用 3.gson将集合转换成json(数组格式)后返回给客户端并显示 教学使用,高手请绕行!
1.11 在spring中获取代理对象代理的目标对象工具类 1.12 如何为spring代理类设置属性值 1.13 我对SpringDAO层支持的总结 1.14 我对SpringDAO层支持的总结 1.15 我对SpringDAO层支持的总结 1.16 我对Spring 容器管理...
使用@RequestBody接收Axios传来的Json对象时,可以用一个Map对象接收,也可以用一个POJO类对象接收 2. 使用@RequestBody接收Axios传来的Json对象时,必须设置请求报文中的Content-Type属性为application/json,...
这个参数指的是微信access_token,在微信控制器类方法中,可以通过设置方法的WeixinContext context参数获取,在web mvc框架中,可以通过WeixinContextHelper类的静态方法getAccessToken(String url)获取。...
许多开发人员使用POST主要有2个原因:明确了数据不能被缓存,或者是避免JSON攻击(JSON返回数组的时候可以被入侵)。 缓存解释 jQuery全局对象里的ajax方法提供了一些options来支持缓存和Conditional GETs功能。 $...
8、改进的依赖反转 (IoC) 支持: Web API 使用 MVC Dependency Resolver 实现的服务定位器模式在不同的场景下来获取实例; 9、基于代码的配置: Web API 单独使用代码完成配置, 从而保证了配置文件的整洁; ...
页面跳转后,使用spring mvc向前端页面传过来一个json对象,要在js中获取后,做处理。 返回的json对象: {"nodes":[{"contactmobile":"15922208502","orderno":"XNH31918062989476864"},{"id":"12198","group":"11...
1、indexedDB(Model) — 前端浏览器对象型数据库,一般我们后台用的数据库都是... 有点像是把一个json对象塞入数据库,是不是很暴力? 2、bootstrap(View) – bootstrap,老实说,我不是很熟悉这个东西,毕竟
Spring3.0是Spring在积蓄了3年之久后,隆重推出的一个重大升级版本,进一步加强了Spring作为Java领域第一开源平台的翘楚地位。 Spring3.0引入了众多Java开发者翘首以盼的新功能和新特性,如OXM、校验及格式化框架...
Spring3.0是Spring在积蓄了3年之久后,隆重推出的一个重大升级版本,进一步加强了Spring作为Java领域第一开源平台的翘楚地位。 Spring3.0引入了众多Java开发者翘首以盼的新功能和新特性,如OXM、校验及格式化框架...