简体   繁体   English

如何通过Ajax将复杂对象发布到MVC 4控制器?

[英]How do I post a complex object to an MVC 4 controller via Ajax?

I have an object called DrawDie with the following properties: 我有一个名为DrawDie的对象, 具有以下属性:

ID
PlantID : int
QtyOnHand : int
SizeUS : double
SizeMetric : double
CaseSize : string
Style : string

I have an object called DieOrder with the following properties: 我有一个名为DieOrder的对象, 具有以下属性:

ID : int
DrawDie : DrawDie
DrawDieID : int
PlantID : int
PurchaseOrder : string
Qty : int

I would like to post my object to an MVC controller via an Ajax request. 我想通过Ajax请求将对象发布到MVC控制器。 I would prefer not to use a 3rd party library. 我宁愿不使用第3方库。 I am trying to post it like this: 我正在尝试像这样发布它:

        var DieOrder = new Object();
        var DrawDie = new Object();

        DieOrder.Qty = $("#Qty").val();
        DieOrder.DrawDieID = $("#DrawDieID").val();


        DrawDie.CaseSize = $("#DrawDie_CaseSize").val();
        DrawDie.Qty = $("#Qty").val();          
        DrawDie.ID = dieID;
        DrawDie.QtyOnHand = 0;
        DrawDie.SizeUS = $("#DrawDie_SizeUS");
        DrawDie.SizeMetric = $("#DrawDie_SizeMetric");
        DrawDie.PlantID = $("#PlantID").val();
        DrawDie.IsTransfer = "N";

        DieOrder.PlantID = $("#PlantID").val();
        DieOrder.OrderedBy = $("#OrderedBy").val();
        DieOrder.PurchaseOrder = $("#PurchaseOrder").val();
        DieOrder.DrawDie = DrawDie;


        $.ajax({
            url: "Order/OrdDie",
            type: 'POST',
            contentType: 'application/json; charset=utf-8',
            data: JSON.stringify(DieOrder),
            success: handleData


        });

    });

I have two questions. 我有两个问题。 One, is my approach way off or is it okay ? 一,我的进场方式是否可行? I am getting an UncaughtType error: Converting circular structure to JSON. 我收到UncaughtType错误:将圆形结构转换为JSON。 How do I restructure my request so I do not get such an error? 我如何重组我的请求,以免出现此类错误? I am unsure of why I am getting this error. 我不确定为什么会收到此错误。

Thanks 谢谢

Update: 更新:

 DrawDie.SizeUS = $("#DrawDie_SizeUS").val();
 DrawDie.SizeMetric = $("#DrawDie_SizeMetric").val();

I was missing the .val() 我错过了.val()

The problem you're really having is that your controller is interpreting all of the contents of your DieOrder object as arguments to your controller action. 您真正遇到的问题是,控制器将DieOrder对象的所有内容解释为控制器操作的参数。

If your controller action looks like this... 如果您的控制器动作如下所示...

[HttpPost]
public void OrdDie(OrderDieViewModel model)
{
    // Some operations
}

...Then the data that your stringifying should look like this: ...然后您要进行分类的数据应如下所示:

{
    'model': DieOrder
}

...so, you're really just missing a step prior to making your $.ajax call: ...因此,您实际上只是在拨打$.ajax电话之前错过了一步:

var DrawDie = {
    // Properties...
};

var DieOrder = {
    // Properties...
    'DrawDie': DrawDie
};

var data = {
    'model': DieOrder
};

$.ajax({
    // Spec object here.
});

In short? 简而言之? Your data object's contents must match the argument list of the action you are calling. 数据对象的内容必须与您要调用的操作的参数列表匹配。 This means that if you pass only a model to your JSON action, your data object must have only one member: the JavaScript representation of that model object. 这意味着,如果仅将模型传递给JSON操作,则数据对象必须只有一个成员:该模型对象的JavaScript表示形式。

Try adding dataType: 'json' to your ajax request: 尝试将dataType:'json'添加到您的Ajax请求中:

$.ajax({
    url: "Order/OrdDie",
    type: 'POST',
    cache: false,
    data: JSON.stringify(_data),
    dataType: 'json',
    contentType: 'application/json',
    success: handleData
});

In addition you can initialize JS objects like this: 另外,您可以像这样初始化JS对象:

var DieOrder = {};
var DrawDie = {};

I hope this help! 希望对您有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM