ASP.NET MVC3 JSON 模型与嵌套类的绑定
在MVC3中,如果模型有嵌套对象,是否可以自动将javascript对象绑定到模型?我的模型如下所示:
public class Tweet
{
public Tweet()
{
Coordinates = new Geo();
}
public string Id { get; set; }
public string User { get; set; }
public DateTime Created { get; set; }
public string Text { get; set; }
public Geo Coordinates { get; set; }
}
public class Geo {
public Geo(){}
public Geo(double? lat, double? lng)
{
this.Latitude = lat;
this.Longitude = lng;
}
public double? Latitude { get; set; }
public double? Longitude { get; set; }
public bool HasValue
{
get
{
return (Latitude != null || Longitude != null);
}
}
}
当我将以下 JSON 发布到我的控制器时,除“坐标”之外的所有内容都会成功绑定:
{"Text":"test","Id":"testid","User":"testuser","Created":"","Coordinates":{"Latitude":57.69679752892457,"Longitude":11.982091465576104}}
这就是我的控制器操作的样子:
[HttpPost]
public JsonResult ReTweet(Tweet tweet)
{
//do some stuff
}
我是否在这里遗漏了某些内容,或者新的自动绑定功能是否仅支持原始对象?
In MVC3, is it possible to automatically bind javascript objects to models if the model has nested objects? My model looks like this:
public class Tweet
{
public Tweet()
{
Coordinates = new Geo();
}
public string Id { get; set; }
public string User { get; set; }
public DateTime Created { get; set; }
public string Text { get; set; }
public Geo Coordinates { get; set; }
}
public class Geo {
public Geo(){}
public Geo(double? lat, double? lng)
{
this.Latitude = lat;
this.Longitude = lng;
}
public double? Latitude { get; set; }
public double? Longitude { get; set; }
public bool HasValue
{
get
{
return (Latitude != null || Longitude != null);
}
}
}
When I post the following JSON to my controller everything except "Coordinates" binds successfully:
{"Text":"test","Id":"testid","User":"testuser","Created":"","Coordinates":{"Latitude":57.69679752892457,"Longitude":11.982091465576104}}
This is what my controller action looks like:
[HttpPost]
public JsonResult ReTweet(Tweet tweet)
{
//do some stuff
}
Am I missing something here or does the new auto-binding feature only support primitive objects?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,您可以使用 ASP.NET MVC3 绑定复杂的 json 对象。
Phil Haack 写了关于它 最近。
您的 Geo 类有问题。
不要使用可为 null 的属性:
这是我用来测试它的 javascript 代码:
更新
我尝试使用模型绑定程序进行一些实验,并且得出了这个似乎有效的解决方案正确地使用可空类型。
我创建了一个自定义模型绑定器:
并且我已经为所有类型的推文注册了它:
Yes, you can bind complex json objects with ASP.NET MVC3.
Phil Haack wrote about it recently.
You've got a problem with your Geo class here.
Don't use nullable properties:
This is the javascript code I've use to test it:
UPDATE
I've tried to do some experiments with model binders and I came out with this solutions which seems to work properly with nullable types.
I've created a custom model binder:
and I've registered it for all types tweet:
我遇到了同样的问题,但就我而言,这与数据从客户端传输的方式有关。确保 AJAX 请求使用正确的标头和格式。例如:
I experienced the same issue, however in my case this was related to how the data was being transmitted from client side. Make sure the AJAX request is using the proper headers and formatting. For example: