使用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方法的属性注入
随机推荐
- IntelliJ IDEA 建空包合并问题。
举例:我想在一个包下,创建2个空子包,这个时候,却无法再IDE里完成. 老是这样子,如果选中dff.sfsdf再右键 创建包的话,结局是再sfsdf下 又创建一个文件夹. 如果右键创建类的话,实际上在 ...
- 使用jsplumb的一些笔记
欢迎就是需要使用jsplumb跟正在使用jsplumb的一起讨论 欢迎私聊 1.关于jsplumb的connection的一些事件 ####connection拖动的事件 instance.bind( ...
- loaded some nib but the view outlet was not set(转载)
当使用 initWithNibName 函数, 并使用 由nib文件生成的ViewController 的view属性时候,遇到这个问题. //load loc.xib UIViewControlle ...
- 查找Mysql慢查询Sql语句
一.MySQL数据库有几个配置选项可以帮助我们及时捕获低效SQL语句 1,slow_query_log 这个参数设置为ON,可以捕获执行时间超过一定数值的SQL语句. 2,long_query_tim ...
- SQL中rownum和order by的执行顺序的问题
在一个SQL中,如果同时使用rownum和order by,会有一个先后顺序的问题. 比如select id1,id2 from t_tablename where rownum<3 order ...
- python学习day4软件目录结构规范
为什么要设计好目录结构? 参考:http://www.cnblogs.com/alex3714/articles/5765046.html "设计项目目录结构",就和"代 ...
- codeforces 286 E. Ladies' Shop (FFT)
E. Ladies' Shop time limit per test 8 seconds memory limit per test 256 megabytes input standard inp ...
- shell如何向python传递参数,shell如何接受python的返回值
1.shell如何向python传递参数 shell脚本 python $sendmailCommandPath $optDate python脚本 lastDateFormat = sys.argv ...
- PHP程序员未来路在何方
PHP 从诞生到现在已经有20多年历史,从Web时代兴起到移动互联网退潮,互联网领域各种编程语言和技术层出不穷, Node.js . GO . Python 不断地在挑战 PHP 的地位.这些技术的推 ...
- Tensorflow学习:(一)tensorflow框架基本概念
一.Tensorflow基本概念 1.使用图(graphs)来表示计算任务,用于搭建神经网络的计算过程,但其只搭建网络,不计算 2.在被称之为会话(Session)的上下文(context)中执行图 ...