使用jQuery异步传递含复杂属性及集合属性的Model到控制器方法
Student类有集合属性Courses,如何把Student连同集合属性Courses传递给控制器方法?
public class Student
{
public string StudentName { get; set; }
public IList<Course> Courses { get; set; }
}
public class Course
{
public string CourseName { get; set; }
}
□ 思路
在前端拼装成与Student吻合、对应的对象,再使用JSON.stringify()或$.toJSON()方法转换成json格式传递给控制器方法,使用$.toJSON()方法需要引用一个jQuery的扩展js文件。
□ Home/Index.cshtml
- @{
- ViewBag.Title = "Index";
- Layout = "~/Views/Shared/_Layout.cshtml";
- }
- <a id="btn" href="javascript:void(0)">走你</a>
- @section scripts
- {
- <script src="~/Scripts/json.js"></script>
- <script type="text/javascript">
- $(function () {
- $('#btn').click(function() {
- var arr = [];
- var obj1 = new Object();
- obj1.CourseName = "语文";
- arr.push(obj1);
- var obj2 = new Object();
- obj2.CourseName = "数学";
- arr.push(obj2);
- var student = {
- StudentName: '小明',
- Courses: arr
- };
- //var json = JSON.stringify(student);
- var json = $.toJSON(student);
- $.ajax({
- url: '@Url.Action("GetStuent","Home")',
- type: 'POST',
- contentType: 'application/json; charset=utf-8',
- data: json,
- dataType: 'json',
- success: function (data) {
- alert(data.StudentName);
- for (i = 0; i < data.Courses.length; i++) {
- alert(data.Courses[i].CourseName);
- }
- }
- });
- });
- });
- </script>
- }
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
□ HomeController
- public ActionResult Index()
- {
- return View();
- }
- [HttpPost]
- public ActionResult GetStuent(Student student)
- {
- return Json(student);
- }
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
□ 把对象转换成json格式的jQuery扩展
- // From: http://www.overset.com/2008/04/11/mark-gibsons-json-jquery-updated/
- (function ($) {
- m = {
- '\b': '\\b',
- '\t': '\\t',
- '\n': '\\n',
- '\f': '\\f',
- '\r': '\\r',
- '"': '\\"',
- '\\': '\\\\'
- },
- $.toJSON = function (value, whitelist) {
- var a, // The array holding the partial texts.
- i, // The loop counter.
- k, // The member key.
- l, // Length.
- r = /["\\\x00-\x1f\x7f-\x9f]/g,
- v; // The member value.
- switch (typeof value) {
- case 'string':
- return r.test(value) ?
- '"' + value.replace(r, function (a) {
- var c = m[a];
- if (c) {
- return c;
- }
- c = a.charCodeAt();
- return '\\u00' + Math.floor(c / 16).toString(16) + (c % 16).toString(16);
- }) + '"' :
- '"' + value + '"';
- case 'number':
- return isFinite(value) ? String(value) : 'null';
- case 'boolean':
- case 'null':
- return String(value);
- case 'object':
- if (!value) {
- return 'null';
- }
- if (typeof value.toJSON === 'function') {
- return $.toJSON(value.toJSON());
- }
- a = [];
- if (typeof value.length === 'number' &&
- !(value.propertyIsEnumerable('length'))) {
- l = value.length;
- for (i = 0; i < l; i += 1) {
- a.push($.toJSON(value[i], whitelist) || 'null');
- }
- return '[' + a.join(',') + ']';
- }
- if (whitelist) {
- l = whitelist.length;
- for (i = 0; i < l; i += 1) {
- k = whitelist[i];
- if (typeof k === 'string') {
- v = $.toJSON(value[k], whitelist);
- if (v) {
- a.push($.toJSON(k) + ':' + v);
- }
- }
- }
- } else {
- for (k in value) {
- if (typeof k === 'string') {
- v = $.toJSON(value[k], whitelist);
- if (v) {
- a.push($.toJSON(k) + ':' + v);
- }
- }
- }
- }
- return '{' + a.join(',') + '}';
- }
- };
- })(jQuery);
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
使用jQuery异步传递含复杂属性及集合属性的Model到控制器方法的更多相关文章
- Struts(二十一):类型转换与复杂属性、集合属性配合使用
背景: 本章节主要以复杂属性.集合属性类型转化为例,来学习这两种情况下怎么使用. 复杂对象属性转换场景: 1.新建struts_04 web.xml <?xml version="1. ...
- 使用jQuery异步传递Model到控制器方法,并异步返回错误信息
需要通过jquery传递到控制器方法的Model为: public class Person { public string Name { get; set; } public int Age { g ...
- XStream别名;元素转属性;去除集合属性(剥皮);忽略不需要元素
city package xstream; public class City { private String name; private String description; public St ...
- 如何利用jQuery post传递含特殊字符的数据【转】
在jQuery中,我们通常利用$.ajax或$.post进行数据传递处理,但这里通常不能传递特殊字符,如:“<”.本文就介绍如何传递这种含特殊字符的数据. 1.准备页面和控制端代码 页面代码如下 ...
- MVC验证11-对复杂类型使用jQuery异步验证
原文:MVC验证11-对复杂类型使用jQuery异步验证 本篇体验使用"jQuery结合Html.BeginForm()"对复杂类型属性进行异步验证.与本篇相关的"兄弟篇 ...
- jQuery异步获取json数据的2种方式
jQuery异步获取json数据有2种方式,一个是$.getJSON方法,一个是$.ajax方法.本篇体验使用这2种方式异步获取json数据,然后追加到页面. 在根目录下创建data.json文件: ...
- Hibernate 集合映射 一对多多对一 inverse属性 + cascade级联属性 多对多 一对一 关系映射
1 . 集合映射 需求:购物商城,用户有多个地址. // javabean设计 // javabean设计 public class User { private int userId; privat ...
- 编写高质量代码改善C#程序的157个建议——建议25:谨慎集合属性的可写操作
建议25:谨慎集合属性的可写操作 如果类型的属性中有集合属性,那么应该保证属性对象是由类型本身产生的.如果将属性设置为可写,则会增加抛出异常的几率.一般情况下,如果集合属性没有值,则它返回的Count ...
- 六 Spring属性注入的四种方式:set方法、构造方法、P名称空间、SPEL表达式
Spring的属性注入: 构造方法的属性注入 set方法的属性注入
随机推荐
- HDU 2609 How many(最小表示+set)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2609 题目大意: 题目大意有n个有01组成的字符串,每个字符串都代表一个项链,那么该字符串就是一个环状 ...
- 编程六月定律 | 外刊IT评论网
编程六月定律 上周,我被迫对一个很老的项目做一些修改.麻烦是,当开始着手时,我真的记不清这个项目究竟有多老了. 这实际上是我使用Codeigniter实现的第一个MVC项目.打开项目文件后,很多东西都 ...
- celery在Django中的集成使用
继上回安装和使用Redis之后,看看如何在Django中使用Celery.Celery是Python开发分布式任务列队的处理库.可以异步分布式地异步处理任务,也可定时执行任务等等.通常我们可以在Dja ...
- mybatis中多条件判断---choose when的用法
<select id="getFunctionByPage" resultMap="FunctionRlt"> SELECT K.FUNCTION_ ...
- 【笔记】Python简明教程
Python简明教程,此资源位于http://woodpecker.org.cn/abyteofpython_cn/chinese/ s=u'中文字符' #u表示unicode,使用u之后能正常显示中 ...
- CCF CSP 201604-4 游戏
CCF计算机职业资格认证考试题解系列文章为meelo原创,请务必以链接形式注明本文地址 CCF CSP 201604-4 游戏 问题描述 小明在玩一个电脑游戏,游戏在一个n×m的方格图上进行,小明控制 ...
- mysql千万级表关联优化
MYSQL一次千万级连表查询优化(一) 概述: 交代一下背景,这算是一次项目经验吧,属于公司一个已上线平台的功能,这算是离职人员挖下的坑,随着数据越来越多,原本的SQL查询变得越来越慢,用户体验特别差 ...
- PHP 识别 java 8位 des 加密和 解密方式
代码及使用说明: <?php /** *PHP 识别 java 8位密钥的加密和解密方式 *@desc 加密方式 通用 */ class DES { var $key; var $iv; //偏 ...
- ASP.NET:MVC模板化机制
模版化的核心是定制ViewEngine.完整的模板化必须实现3个功能:1.网站的多套模版(razor)支持 2.模版的多样式(style)支持. 3.按需实现模版:没有实现的加载默认模版. 我们除了要 ...
- USACO 6.3 Fence Rails(一道纯剪枝应用)
Fence RailsBurch, Kolstad, and Schrijvers Farmer John is trying to erect a fence around part of his ...