javascript:带有序列化数组的json对象,可在.NET MVC操作中使用 - c#

简单的问题:

我想将一些数据发布到这样的操作中:

        var data = {
            Prop1 : 'a',
            ListOfObjects: [{ PropertyA: 1, PropertyB: 2 }, { PropertyA: 3, PropertyB: 4}]
        };

当我通过JQuery AJAX将此数据发送到Action时,我的模型被部分填充:

public class MyObject{
    public int PropertyA {get;set;}
    public int PropertyB {get;set;}
}

public class MyModel{
    public string Prop1 {get;set;}
    public List<MyObject> ListOfObjects {get;set;}
}

public JsonResult Save(MyModel model)
.
.
.
model.Prop1 //Is okay!
model.ListOfObjects[0] // is okay too...List has 2 items
model.ListOfObjects[0].PropertyA; //Nope...no values inside this model...

我想原因是,序列化的HTTP数据是错误的,它们就像
ListOfObjects [0] [PropertyA] <-,但应为ListOfObjkects [0] .PropertyA

有谁知道该怎么办?

编辑:
我的JQuery AJAX代码:

$.ajax({
                    type: 'POST',
                    url: saveURL,
                    dataType: 'json',
                    data: data,
                    complete: function () {
                        DeleteMainLoader();
                    },
                    success: function success(data, textStatus, jqXHR) {
                        if (data.success) {
                            alert('win!')
                        }
                        else {
                            alert('error');
                        }

                    },
                    error: function (jqXHR, textStatus, errorThrown) {
                        alert('errrrrooooorrrr');
                    }
                });

参考方案

如果您使用的是ASP.NET MVC 3或更高版本,则最简单的方法是发送JSON而不是URL编码的数据。这使复杂的嵌套对象易于使用,因为jQuery当时不需要了解任何ASP.NET MVC的约定。

为此,只需建立一个与服务器端类匹配的对象,对其进行字符串化,并确保在请求上将内容类型设置为application/json

var data = {
  Prop1 : 'a',
  ListOfObjects: [{ PropertyA: 1, PropertyB: 2 }, { PropertyA: 3, PropertyB: 4}]
};

$.ajax({
  type: 'POST',
  url: saveURL,
  // This is unnecessary; jQuery will detect the response's dataType based
  //  on its Content-Type header automatically.
  dataType: 'json',
  // This sets the request's Content-Type to let MVC know how to interpret the
  //  data parameter.
  contentType: 'application/json',
  // I can't remember if this is 100% necessary in this case, but some ASP.NET 
  //  endpoints only work if you match up the method's parameter name like this.
  data: JSON.stringify({ model: data })
});

javascript popupwindow之后的行如何工作? - javascript

好的,我有一个来自后面代码的方法,可以创建一个popupwindow。然后有一行代码要在那之后执行,我想知道那行代码何时执行,是在使用popupwindow之后执行还是在创建popupwindow之后执行?例如:void exPopupWindowMethod() { string scr = "window.open('exampleP…

获取JavaScript值到C#字符串 - javascript

                        是否可以在C#中执行类似的操作?该值为“ 10/05/2014”string jsValue = javascript("$('#EstimatedStartDate').val()"); 参考方案 您能否更详细地阐明您要做什么。看来您正在尝试从javascript(客户…

如何使用JavaScript访问嵌入式ASP.NET GlobalResources? - javascript

我正在开发一个遗留的ASP.NET项目,该项目正试图缓慢地进行调整,但是如果没有像巧克力手指屋一样塌陷的情况,我将无法进行重大更改。我试图为此找到解决方案,但由于术语的特定混合(“ javascript”,“ embedded”和/或“ resource”只是为我提供了有关如何嵌入.js文件的信息,而失败了)。 。),这可能是一种怪异的处理方式。该项目将Ap…

ddl在服务器中未更新-asp.net - javascript

我在ASP.NET c#上工作。我有一个DropDownList。 (runat =“ server”)在$ {document).ready上,我更新了它的值:$(document).ready(function () { document.getElementById("ddl").value = "abc"; ……

从AngularJS发回数据到ASP.NET - javascript

我是新的编码人员,所以这是我的问题,我正在通过c#和angularJs从数据库中获取数据,我可以修改数据,并想在需要将其保存在数据库中后发回,但我要一步一步,所以这是我的代码,非常感谢。我希望它是西班牙文,不要介意。var datosActuales = { 'id': $scope.datosActualizados.id, '…