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

  1. @{
  1. ViewBag.Title = "Index";
  1. Layout = "~/Views/Shared/_Layout.cshtml";
  1. }
  1.  
  1. <a id="btn" href="javascript:void(0)">走你</a>
  1.  
  1. @section scripts
  1. {
  1. <script src="~/Scripts/json.js"></script>
  1. <script type="text/javascript">
  1. $(function () {
  1. $('#btn').click(function() {
  1. var arr = [];
  1.  
  1. var obj1 = new Object();
  1. obj1.CourseName = "语文";
  1. arr.push(obj1);
  1.  
  1. var obj2 = new Object();
  1. obj2.CourseName = "数学";
  1. arr.push(obj2);
  1.  
  1. var student = {
  1. StudentName: '小明',
  1. Courses: arr
  1. };
  1.  
  1. //var json = JSON.stringify(student);
  1. var json = $.toJSON(student);
  1.  
  1. $.ajax({
  1. url: '@Url.Action("GetStuent","Home")',
  1. type: 'POST',
  1. contentType: 'application/json; charset=utf-8',
  1. data: json,
  1. dataType: 'json',
  1. success: function (data) {
  1. alert(data.StudentName);
  1. for (i = 0; i < data.Courses.length; i++) {
  1. alert(data.Courses[i].CourseName);
  1. }
  1. }
  1. });
  1. });
  1. });
  1. </script>
  1. }
  1.  

.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

  1. public ActionResult Index()
  1. {
  1. return View();
  1. }
  1.  
  1. [HttpPost]
  1. public ActionResult GetStuent(Student student)
  1. {
  1. return Json(student);
  1. }

.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扩展

  1. // From: http://www.overset.com/2008/04/11/mark-gibsons-json-jquery-updated/
  1.  
  1. (function ($) {
  1. m = {
  1. '\b': '\\b',
  1. '\t': '\\t',
  1. '\n': '\\n',
  1. '\f': '\\f',
  1. '\r': '\\r',
  1. '"': '\\"',
  1. '\\': '\\\\'
  1. },
  1. $.toJSON = function (value, whitelist) {
  1. var a, // The array holding the partial texts.
  1. i, // The loop counter.
  1. k, // The member key.
  1. l, // Length.
  1. r = /["\\\x00-\x1f\x7f-\x9f]/g,
  1. v; // The member value.
  1.  
  1. switch (typeof value) {
  1. case 'string':
  1. return r.test(value) ?
  1. '"' + value.replace(r, function (a) {
  1. var c = m[a];
  1. if (c) {
  1. return c;
  1. }
  1. c = a.charCodeAt();
  1. return '\\u00' + Math.floor(c / 16).toString(16) + (c % 16).toString(16);
  1. }) + '"' :
  1. '"' + value + '"';
  1.  
  1. case 'number':
  1. return isFinite(value) ? String(value) : 'null';
  1.  
  1. case 'boolean':
  1. case 'null':
  1. return String(value);
  1.  
  1. case 'object':
  1. if (!value) {
  1. return 'null';
  1. }
  1. if (typeof value.toJSON === 'function') {
  1. return $.toJSON(value.toJSON());
  1. }
  1. a = [];
  1. if (typeof value.length === 'number' &&
  1. !(value.propertyIsEnumerable('length'))) {
  1. l = value.length;
  1. for (i = 0; i < l; i += 1) {
  1. a.push($.toJSON(value[i], whitelist) || 'null');
  1. }
  1. return '[' + a.join(',') + ']';
  1. }
  1. if (whitelist) {
  1. l = whitelist.length;
  1. for (i = 0; i < l; i += 1) {
  1. k = whitelist[i];
  1. if (typeof k === 'string') {
  1. v = $.toJSON(value[k], whitelist);
  1. if (v) {
  1. a.push($.toJSON(k) + ':' + v);
  1. }
  1. }
  1. }
  1. } else {
  1. for (k in value) {
  1. if (typeof k === 'string') {
  1. v = $.toJSON(value[k], whitelist);
  1. if (v) {
  1. a.push($.toJSON(k) + ':' + v);
  1. }
  1. }
  1. }
  1. }
  1. return '{' + a.join(',') + '}';
  1. }
  1. };
  1.  
  1. })(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到控制器方法的更多相关文章

  1. Struts(二十一):类型转换与复杂属性、集合属性配合使用

    背景: 本章节主要以复杂属性.集合属性类型转化为例,来学习这两种情况下怎么使用. 复杂对象属性转换场景: 1.新建struts_04 web.xml <?xml version="1. ...

  2. 使用jQuery异步传递Model到控制器方法,并异步返回错误信息

    需要通过jquery传递到控制器方法的Model为: public class Person { public string Name { get; set; } public int Age { g ...

  3. XStream别名;元素转属性;去除集合属性(剥皮);忽略不需要元素

    city package xstream; public class City { private String name; private String description; public St ...

  4. 如何利用jQuery post传递含特殊字符的数据【转】

    在jQuery中,我们通常利用$.ajax或$.post进行数据传递处理,但这里通常不能传递特殊字符,如:“<”.本文就介绍如何传递这种含特殊字符的数据. 1.准备页面和控制端代码 页面代码如下 ...

  5. MVC验证11-对复杂类型使用jQuery异步验证

    原文:MVC验证11-对复杂类型使用jQuery异步验证 本篇体验使用"jQuery结合Html.BeginForm()"对复杂类型属性进行异步验证.与本篇相关的"兄弟篇 ...

  6. jQuery异步获取json数据的2种方式

    jQuery异步获取json数据有2种方式,一个是$.getJSON方法,一个是$.ajax方法.本篇体验使用这2种方式异步获取json数据,然后追加到页面. 在根目录下创建data.json文件: ...

  7. Hibernate 集合映射 一对多多对一 inverse属性 + cascade级联属性 多对多 一对一 关系映射

    1 . 集合映射 需求:购物商城,用户有多个地址. // javabean设计 // javabean设计 public class User { private int userId; privat ...

  8. 编写高质量代码改善C#程序的157个建议——建议25:谨慎集合属性的可写操作

    建议25:谨慎集合属性的可写操作 如果类型的属性中有集合属性,那么应该保证属性对象是由类型本身产生的.如果将属性设置为可写,则会增加抛出异常的几率.一般情况下,如果集合属性没有值,则它返回的Count ...

  9. 六 Spring属性注入的四种方式:set方法、构造方法、P名称空间、SPEL表达式

    Spring的属性注入: 构造方法的属性注入 set方法的属性注入

随机推荐

  1. HDU 2609 How many(最小表示+set)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2609 题目大意: 题目大意有n个有01组成的字符串,每个字符串都代表一个项链,那么该字符串就是一个环状 ...

  2. 编程六月定律 | 外刊IT评论网

    编程六月定律 上周,我被迫对一个很老的项目做一些修改.麻烦是,当开始着手时,我真的记不清这个项目究竟有多老了. 这实际上是我使用Codeigniter实现的第一个MVC项目.打开项目文件后,很多东西都 ...

  3. celery在Django中的集成使用

    继上回安装和使用Redis之后,看看如何在Django中使用Celery.Celery是Python开发分布式任务列队的处理库.可以异步分布式地异步处理任务,也可定时执行任务等等.通常我们可以在Dja ...

  4. mybatis中多条件判断---choose when的用法

    <select id="getFunctionByPage" resultMap="FunctionRlt"> SELECT K.FUNCTION_ ...

  5. 【笔记】Python简明教程

    Python简明教程,此资源位于http://woodpecker.org.cn/abyteofpython_cn/chinese/ s=u'中文字符' #u表示unicode,使用u之后能正常显示中 ...

  6. CCF CSP 201604-4 游戏

    CCF计算机职业资格认证考试题解系列文章为meelo原创,请务必以链接形式注明本文地址 CCF CSP 201604-4 游戏 问题描述 小明在玩一个电脑游戏,游戏在一个n×m的方格图上进行,小明控制 ...

  7. mysql千万级表关联优化

    MYSQL一次千万级连表查询优化(一) 概述: 交代一下背景,这算是一次项目经验吧,属于公司一个已上线平台的功能,这算是离职人员挖下的坑,随着数据越来越多,原本的SQL查询变得越来越慢,用户体验特别差 ...

  8. PHP 识别 java 8位 des 加密和 解密方式

    代码及使用说明: <?php /** *PHP 识别 java 8位密钥的加密和解密方式 *@desc 加密方式 通用 */ class DES { var $key; var $iv; //偏 ...

  9. ASP.NET:MVC模板化机制

    模版化的核心是定制ViewEngine.完整的模板化必须实现3个功能:1.网站的多套模版(razor)支持 2.模版的多样式(style)支持. 3.按需实现模版:没有实现的加载默认模版. 我们除了要 ...

  10. USACO 6.3 Fence Rails(一道纯剪枝应用)

    Fence RailsBurch, Kolstad, and Schrijvers Farmer John is trying to erect a fence around part of his ...