admin 管理员组文章数量: 1086019
My controller is returning an object graph to the view in json format like this
return Json(customer);
On the view my json object looks like this
{
Name: 'Joe',
Budget: { Amount: 500, Spend: 100 }
}
Which maps correctly to my customer object:
public class Customer
{
public string Name {get;set;}
public Budget Budget{get;set;}
}
public class Budget
{
public decimal Amount{get;set;}
public decimal Spend{get;set;}
}
I want to pass that same json object back to another method on the controller with this signature:
public ActionResult Method(Customer customer)
When I do this customer's name get populated but not the Budget class, which I understand why because the modelbinder is expecting this: {Name:'Joe','Budget.Amount':500,'Budget.Spend': 100}
So I have to options: 1. I can return the json object in the format it wants, but I don't know how because you can't do this:
return Json(new { Budget.Amount= 500})
- I can flatten the json object on the client side. Is there plugins or methods to do this?
My controller is returning an object graph to the view in json format like this
return Json(customer);
On the view my json object looks like this
{
Name: 'Joe',
Budget: { Amount: 500, Spend: 100 }
}
Which maps correctly to my customer object:
public class Customer
{
public string Name {get;set;}
public Budget Budget{get;set;}
}
public class Budget
{
public decimal Amount{get;set;}
public decimal Spend{get;set;}
}
I want to pass that same json object back to another method on the controller with this signature:
public ActionResult Method(Customer customer)
When I do this customer's name get populated but not the Budget class, which I understand why because the modelbinder is expecting this: {Name:'Joe','Budget.Amount':500,'Budget.Spend': 100}
So I have to options: 1. I can return the json object in the format it wants, but I don't know how because you can't do this:
return Json(new { Budget.Amount= 500})
- I can flatten the json object on the client side. Is there plugins or methods to do this?
- but you can do this, right? not sure if this solves your entire problem: return Json(new { Budget = new { Amount = 500 }}) – RationalGeek Commented Feb 5, 2010 at 21:43
- i am trying to flatten json object so that the result is '{ 'Budget.Amount' = 500 }' – adriaanp Commented Feb 5, 2010 at 21:47
3 Answers
Reset to default 5Here's a function that convert an object to a flat hash
function flatten(json){
var nj = {},
walk = function(j){
var jp;
for(var prop in j){
jp = j[prop];
if(jp.toString() === "[object Object]"){
walk(jp);
}else{
nj[prop] = jp;
}
}
};
walk(json);
return nj;
}
Protovis has a JavaScript flattener, available under the BSD License.
In my case we have solved it by passing additional object to the action url.
public ActionResult Method(Customer customer, [Bind(Prefix="Budget")]Budget budget)
to make this happen you have to flatten the json data (before you sent it to controller) in following way:
How to pass plex type using json to ASP.NET MVC controller
本文标签: javascriptFlattening a complex json object for mvc bindingStack Overflow
版权声明:本文标题:javascript - Flattening a complex json object for mvc binding - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744012519a2518415.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论