Post Complex JavaScript Objects to ASP.NET MVC Controllers
http://www.nickriggs.com/posts/post-complex-javascript-objects-to-asp-net-mvc-controllers/
Post Complex JavaScript Objects to ASP.NET MVC Controllers
Posted in ASP.NET’JavaScript August 21, 2009
Use the plug-in postify.js to handle posting complex JavaScript objects to ASP.NET MVC controllers using the default model binder
There is a lot on conversation going on about binding complex JavaScript objects to ASP.NET MVC actions. Complex objects are objects that have sub objects and/or arrays.
Let’s assume the following complex model:
I have a Person object with some properties, an array of phone numbers and an Address object. I would like to pass a JavaScript representation of this object to our Controller’s Create action:
1
2
3
4
5
6
7
8
[AcceptVerbs(HttpVerbs.Post)]
public
ActionResult Create(Person person)
{
//Code to add the person goes here
//return the view
return
View();
}
On the client, the JavaScript representation of a Person would be:
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
var
myPerson = {
FirstName:
"Nick"
,
LastName:
"Riggs"
,
Age: 29,
Address: {
Street:
"2780 Somewhere Far"
,
City:
"Birmingham"
,
State:
"AL"
},
PhoneNumbers: [
"205-555-5634"
,
"205-555-2294"
,
"205-555-7681"
]
};
One way to send this object to our Controller is to “stringify” the object into a JSON string using a plugin like toJSON. However, this requires us to change the Action to accept a string instead of a typed parameter, and then deserialize the string using the JavaScriptSerializer. I can get around this by automating the deserialization with a custom ActionFilterAttribute or ModelBinder. But, what if I want to use the built-in DefaultModelBinder functionality?
The default model binding in ASP.NET MVC works based on form post data. For example, if I were going to post a simple version of Person and have ASP.NET MVC map it to our action’s person parameter, I could post:
1
2
3
person.FirstName: Nick
person.LastName: Riggs
person.Age: 29
ASP.NET MVC does a good job of recognizing this post data as being a Person and mapping it as such. On top of that, it has its own simple yet powerful syntax for representing more complex objects, such as this:
1
2
3
4
5
6
7
8
9
person.FirstName: Nick
person.LastName: Riggs
person.Age: 29
person.PhoneNumbers[0]: 205-555-5634
person.PhoneNumbers[1]: 205-555-5634
person.PhoneNumbers[2]: 205-555-5634
person.Address.Street: 2780 Somewhere Far
person.Address.City: Birmingham
person.Address.State: AL
So, instead of stringifying my JavaScript objects, I will postify them! (I made the word postify™ up, it’s mine now). My custom postify plug-in will do the work. Here is the source code:
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
$.postify =
function
(value) {
var
result = {};
var
buildResult =
function
(object, prefix) {
for
(
var
key
in
object) {
var
postKey = isFinite(key)
? (prefix !=
""
? prefix :
""
) +
"["
+ key +
"]"
: (prefix !=
""
? prefix +
"."
:
""
) + key;
switch
(
typeof
(object[key])) {
case
"number"
:
case
"string"
:
case
"boolean"
:
result[postKey] = object[key];
break
;
case
"object"
:
if
(object[key].toUTCString)
result[postKey] = object[key].toUTCString().replace(
"UTC"
,
"GMT"
);
else
{
buildResult(object[key], postKey !=
""
? postKey : key);
}
}
}
};
buildResult(value,
""
);
return
result;
};
This is the first cut of the plug-in, and I’m sure it’s missing something – I’ll update the source code as I make updates. That said, the plug-in greatly simplifies posting complex objects to ASP.NET MVC controllers. Here is a sample in jQuery that posts myPerson:
1
2
3
4
5
$.ajax({
type:
"POST"
,
url:
"/People/Create"
,
data: $.postify(myPerson)
});
That’s it! The plugin will handle formatting the data in an ASP.NET MVC post-friendly manner. On the server side, the parameter inflates nicely using the default model binder:
If you need to post to an action that takes multiple parameters, the complex object must be prefixed with the name of the parameter – in our case, Person. To include another parameter, use this syntax:
1
2
3
4
5
6
7
8
$.ajax({
type:
"POST"
,
url:
"/JSON/DoSomething"
,
data: $.postify({
person: myPerson,
otherParam:
true
})
});
Post Complex JavaScript Objects to ASP.NET MVC Controllers的更多相关文章
- ASP.NET MVC Controllers and Actions
MVC应用程序里的URL请求是通过控制器Controller处理的,不管是请求视图页面的GET请求,还是传递数据到服务端处理的Post请求都是通过Controller来处理的,先看一个简单的Contr ...
- [引]ASP.NET MVC 4 Content Map
本文转自:http://msdn.microsoft.com/en-us/library/gg416514(v=vs.108).aspx The Model-View-Controller (MVC) ...
- SignalR + KnockoutJS + ASP.NET MVC 实现井字游戏
SignalR + KnockoutJS + ASP.NET MVC 实现井字游戏 1.1.1 摘要 今天,我们将使用SignalR + KnockoutJS + ASP.NET MVC实现一个实 ...
- A Look at the Razor View Engine in ASP.NET MVC
The biggest architectural difference that exists between ASP.NET MVC and ASP.NET Web Forms is the ne ...
- ASP.NET MVC HttpVerbs.Delete/Put Routes not firing
原文地址: https://weblog.west-wind.com/posts/2015/Apr/09/ASPNET-MVC-HttpVerbsDeletePut-Routes-not-firing ...
- ASP.NET MVC使用Bootstrap系列(4)——使用JavaScript插件
阅读目录 序言 Data属性 VS 编程API 下拉菜单(dropdown.js) 模态框(modal.js) 标签页(tab.js) 工具提示(tooltip.js) 弹出框(popover.js) ...
- 转载:Unobtrusive JavaScript in ASP.NET MVC 3 隐式的脚本在MVC3
Unobtrusive JavaScript 是什么? <!--以下是常规Javascript下写出来的Ajax--> <div id="test"> &l ...
- [asp.net mvc 奇淫巧技] 05 - 扩展ScriptBundle,支持混淆加密javascript
一.需求: 在web开发中,经常会处理javascript的一些问题,其中就包括js的压缩,合并,发布版本以及混淆加密等等问题.在asp.net 开发中我们使用ScriptBundle已经可以解决ja ...
- ASP.NET MVC Bundles 用法和说明(打包javascript和css)
本文主要介绍了ASP.NET MVC中的新功能Bundles,利用Bundles可以将javascript和css文件打包压缩,并且可以区分调试和非调试,在调试时不进行压缩,以原始方式显示出来,以方便 ...
随机推荐
- jQuery轮播图
yii2 轮播 样式: <style type="text/css"> *{margin:0;padding:0} body{margin:50px} li{list- ...
- 开源软件项目管理系统招设计/开发。。。。。Zend Framework2架构 svn://735.ikwb.com/pms
开源软件项目管理系统招设计/开发.....Zend Framework2架构svn://735.ikwb.com/pms
- iOS传值方式:属性,代理,block,单例,通知
正向传值均可,反向传值除属性传值不可,其余均可.下面简单介绍: (一)属性传值 第二个界面中的lable显示第一个界面textField中的文本 首先我们建立一个RootViewControllers ...
- 【转】Web应用的组件化开发(一)
原文转自:http://blog.jobbole.com/56161/ 基本思路 1. 为什么要做组件化? 无论前端也好,后端也好,都是整个软件体系的一部分.软件产品也是产品,它的研发过程也必然是有其 ...
- iis6.0+.net 4.0 +mvc 404错误
ps: 在iis中重新注册.net framework命令cd C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis.exe -i 1 ...
- (实用篇)php无限遍历目录
使用的函数有: isset()判断某个变量是否定义 chdir() 将当前目录改变为指定的目录. opendir() 打开目录. readdir()读取目录. getcwd().获取当前目录. 还用到 ...
- easyUI——datebox验证和自定义取消按钮
来源:http://blog.csdn.net/liusong0605/article/details/42270463 1. datebox验证 验证结束时间<起始时间: 起始时 ...
- 使用apt-get方式为Kubuntu安装PHP+MYSQL+Apache
相信很多搭过动态网站的朋友都知道怎么搭web服务器, 本人是linux新手, 以前在windows是直接使用集成的wamp server, 所以在linux没有亲手搭过. 本系统: ubuntu 12 ...
- ZOJ 1049 I Think I Need a Houseboat
原题链接 题目大意:Fred想在Louisiana买一套房子,但是堤坝不牢固,每年都要被河水侵蚀50平方英里.题目给出他豪宅的坐标,要求他被迫移民搬迁的年份. 解法:也没什么好说的,先求出两点间的距离 ...
- 什么是html5
HTML5是用于取代1999年所制定的 HTML 4.01 和 XHTML 1.0 标准的 HTML 标准版本,现在仍处于发展阶段,但大部分浏览器已经支持某些 HTML5 技术.HTML 5有两大特点 ...