English 中文(简体)
asp.net mvc 3 ViewModel集合属性到json不起作用
原标题:asp.net mvc 3 ViewModel collection property to json not working

大家晚上好。我目前正在使用MVC 3,并且我有一个视图模型,它包含一个List属性。我目前正在使用json2的JSON.stringify方法将我的视图模型传递给我的操作方法。在调试时,我注意到所有简单的属性都通过了,但collection属性是空的,尽管我确信集合中至少有一个对象。我想知道是否有人遇到了同样的问题。以下是我用来发布到操作方法的代码:

$.post("/ReservationWizard/AddVehicleToReservation/",
        JSON.stringify( @ViewData["modelAsJSON"] ),
        function (data) {
            if (data != null) {
                $("#vehicle-selection-container").html(data);
                $(".reservation-wizard-step").fadeIn();
            }
        });

The object @ViewData["modelAsJSON"] contains the following json and is passed to my action method {"NumberOfVehicles":1,"VehiclesToService":[{"VehicleMakeId":0,"VehicleModelId":0}]}

正如您所看到的,属性“VehiclesToService”有一个对象,但当它到达我的操作方法时,它不会被转换为集合中的相应对象,而是集合是空的。

如果有人对这个问题有任何见解,我们将不胜感激。

提前谢谢。

UPDATE

OK after making the recommended changes and making the call to new JavaScriptSerializer().Serialize(@Model) this is the string that ultimately gets sent to my action method through the post {"NumberOfVehicles":1,"VehiclesToService":[{"VehicleMakeId":0,"VehicleModelId":0}]}

我可以调试并查看发送到我的操作方法的对象,但集合属性也是空的,我确信集合中至少有一个对象。

The AddVehicleToReservation action method is declared as follows: public ActionResult AddVehicleToReservation(VehicleSelection selections) { ... return PartialView("viewName", model); }

最佳回答

问题是:

JSON.stringify( @ViewData["modelAsJSON"] )

<code>JSON.stringfy</code>是一个客户端函数,您将作为参数传递一个存储在ViewData中的列表,因此我认为它最终调用<code>.ToString()</code

JSON.stringify( System.Collections.Generic.List<Foo> )

这显然没有多大意义。另外,不要忘记,为了使用$.post函数将参数传递给服务器,第二个参数需要是一个javascript对象,而不是JSON.stringify所做的(它生成一个字符串)。因此,您最终需要使用这样的HTML:

$.post(
     ReservationWizard/AddVehicleToReservation ,
    [ { id: 1, title:  title 1  }, { id: 2, title:  title 2  } ],
    function (data) {
        if (data != null) {
            $( #vehicle-selection-container ).html(data);
            $( .reservation-wizard-step ).fadeIn();
        }
    }
);

因此,要实现这一点,您首先需要将此ViewData序列化为JSON。您可以使用JavaScriptSerializer类:

@{
    var myList = new JavaScriptSerializer().Serialize(ViewData["modelAsJSON"]);
}
$.post(
     @Url.Action("AddVehicleToReservation", "ReservationWizard") ,
    // Don t use JSON.stringify: that makes JSON request and without
    // proper content type header your sever won t be able to bind it
    @myList,
    function (data) {
        if (data != null) {
            $( #vehicle-selection-container ).html(data);
            $( .reservation-wizard-step ).fadeIn();
        }
    }
);

请不要使用此ViewData。使视图具有强类型并使用视图模型。

问题回答

暂无回答




相关问题
JQuery/MVC Search Issue

I have inherited a piece of work where the entry screen shows a summary of 20 calculated variables. E.g. Var A (250), Var B (79). Clicking on any of these links takes the user to a view with a ...

jQuery quicksearch plug-in tinkering with JSON

I ve implemented the quicksearch plugin by Rik Lomas and I love it for an application in a custom CMS I m building. I was wondering though, since I m going to have a bizillion items in the table if ...

JSON with classes?

Is there a standardized way to store classes in JSON, and then converting them back into classes again from a string? For example, I might have an array of objects of type Questions. I d like to ...

PHP json_decode question

i m trying to use json_decode to combine a few json objects and then re-encode it. my json looks like: { "core": { "segment": [ { "id": 7, "...

Converting JSON data to Java object

I want to be able to access properties from a JSON string within my Java action method. The string is available by simply saying myJsonString = object.getJson(). Below is an example of what the string ...

热门标签