gpt4 book ai didi

asp.net-mvc - Json ModelBinding 在 MVC3 RC2 中无法通过 Ajax 工作

转载 作者:行者123 更新时间:2023-12-01 04:28:24 25 4
gpt4 key购买 nike

我尝试在 MVC3 RC2 中使用带有 JSON 模型绑定(bind)的 Ajax,但由于某种原因,数据无法到达 Controller 操作方法。还有其他关于此主题的帖子,但有些提到了旧版本的 MVC,而我发现的这些帖子都没有切中要害。由于我是 JSON、JSON ModelBinding 的新手,并且在 jQuery Ajax 调用方面经验不足,因此我可能很容易错过一些东西。

我的起点是 Scott Guthrie 的帖子 http://bit.ly/btdFP5 Javascript 和 AJAX 改进部分介绍了 MVC3 预览,但我从 Google API 中提取 map 数据,因此我的代码略有不同:

        var bounds = map.getBounds();
var southWest = bounds.getSouthWest();
var northEast = bounds.getNorthEast();

var mb = { nelat: northEast.lat(), nelng: northEast.lng(), swlat: southWest.lat(), swlng: southWest.lng() };

$.ajax(
{
url: "/home/GetMarkers",
type: "post",
dataType: "json",
data:JSON.stringify(mb),
contentTYpe: "application/json; charset=utf-8",
success: function (result) {
...

success 函数一切正常,因此我已停止在那里列出代码。

我在项目的域模型中有一个用于纬度/经度坐标的类:

Public Class MapBounds
Public Property nelat As Double
Public Property nelng As Double
Public Property swlat As Double
Public Property swlng As Double
End Class

Controller 操作是:

Function GetMarkers(ByVal mb As MapBounds) As JsonResult
Dim objMarkers = ... get relevant data from database
Return Json(objMarkers)
End Function

通过 Firebug 观察到的发布的 JSON 数据(通常)是:

{"nelat":51.22959997248028,"nelng":9.811035156249996,
"swlat":42.190280664203,"swlng":-7.8110351562500036}

但是MapBounds对象中的属性返回的值全部为零。我尝试了传递参数的各种变体,包括传递单个值,但没有运气。这些值为零或 null。

Phil Haack 的帖子 - http://bit.ly/bUl21b - 在将 JSON 发送到操作方法时,参数表示 JsonValueProviderFactory 现在是 MVC3 的一部分,但我还是检查了一下它是否存在(确实如此)。

最佳答案

您的 contentType 参数中有拼写错误,这可能会导致其无法正确设置。它应该是 contentType 而不是 contentTYpe

关于asp.net-mvc - Json ModelBinding 在 MVC3 RC2 中无法通过 Ajax 工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4635708/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com