Json ModelBinding无法在MVC3 RC2中使用Ajax

时间:2011-01-08 19:29:46

标签: asp.net-mvc json jquery asp.net-mvc-3

我正在尝试在MVC3 RC2中使用Ajax和JSON模型绑定,并且出于某种原因,数据没有通过控制器操作方法。关于这个主题还有其他帖子,但有些帖子提到了MVC的旧版本,而且我发现的那些帖子都没有到达现场。由于我是JSON,JSON ModelBinding的新手,并且在jQuery Ajax调用方面没有那么多经验,我可能很容易错过一些东西。

我的出发点是Scott Guthrie的帖子http://bit.ly/btdFP5 Javascript和AJAX改进部分介绍了MVC3预览,但我将地图数据从Google API中提取出来,所以我的代码是略有不同:

        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) {
                ...

成功功能一切正常,所以我已停止在那里列出代码。

我在项目的域模型中有一个类用于lat / long坐标:

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的一部分,但无论如何我检查它是否存在(和它是)。

1 个答案:

答案 0 :(得分:2)

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

相关问题