1

我有一个 Ajax 调用,它将对象列表发送到控制器中的方法。
我查看了它发送的 json,一切似乎都井井有条。但是当它到达控制器时,列表就在那里,具有正确数量的对象,但它们的所有属性都是空的,即使值在 json 中正确设置。

示例:我有一个包含 10 个对象的列表,所有对象的属性都设置了特定值。我执行了调用,但是当列表到达控制器时,它有 10 个对象,它们的所有属性都设置为空。

有人知道为什么会这样吗?

这是电话:
由于大量数据,我不得不使用 post 而不是 get。

$("#testeFA").click(function()
{
    <% JavaScriptSerializer serializador = new JavaScriptSerializer(); %>

    var models = <%: MvcHtmlString.Create(serializador.Serialize(apontamentos)) %>
    //"apontamentos" is the name of the List<ApontamentoModel>

    $.post('<%: Url.Action("GeraFA") %>', { models: models }, function (sucesso)
    {
        //do whatever
    }, 'json');
});

这是方法:

public JsonResult GeraFA(List<ApontamentoModel> models) <- this is where the list shows all the objects' properties as null
{
    JsonResult result = new JsonResult();

    //do whatever

    result.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
    return result;
 }

这是json的一部分,所以你可以看到结构:

[{"DocEntry":1,
  "LineID":5,
  "Data":"01/06/2012",
  "HoraInicial":
      {"Ticks":288000000000,
       "Days":0,
       "Hours":8,
       "Milliseconds":0,
       "Minutes":0,
       "Seconds":0,
       "TotalDays":0.33333333333333331,
       "TotalHours":8,
       "TotalMilliseconds":28800000,
       "TotalMinutes":480,
       "TotalSeconds":28800},
  "CodigoCliente":"C00013",
  "Cliente":"Client Name",
  "CodigoProjeto":283,
  "Projeto":"Project Name",
  "CodigoServico":18,
  "TipoServico":"",
  "CodigoDespesa":0,
  "Despesa":"",
  "Quantidade":0,
  "NumeroChamado":0,
  "NumeroFA":10,
  "Apropria":true,
  "Narrativa":"teste",
  "NomeConsultor":"Name"},
 {"DocEntry":1,
  "LineID":13 //and so on to all the other elements

我正在使用 MVC2

4

1 回答 1

1

ASP.NET MVC 2 doesn't support JSON requests out of the box. This functionality has been built-in ASP.NET MVC 3. You could though write a custom JsonValueProviderFactory to achieve that. Phil Haack wrote an excellent blog post on this topic. You also need to use $.ajax instead of $.post as illustrated by Phil in order to be able to set the application/json request Content-Type header.

So once you download and register the JsonValueProviderFactory in your application you could:

$('#testeFA').click(function() {
    <% JavaScriptSerializer serializador = new JavaScriptSerializer(); %>
    //"apontamentos" is the name of the List<ApontamentoModel>
    var models = <%= serializador.Serialize(apontamentos) %>;
    $.ajax({
        url: '<%= Url.Action("GeraFA") %>',
        type: 'post',
        contentType: 'application/json;charset=utf-8',
        data: JSON.stringify({ models: models }),
        success: function(result) {
            //do whatever
        }
    });
});

Also notice the usage of the JSON.stringify method which converts the javascript models variable into a JSON string to be sent to the server as specified by the contentType parameter. This method is natively built-in all modern browsers. If you need to support some legacy browsers you could include the json2.js script to your page.

于 2012-06-15T18:59:55.423 回答