如何通过json对象数组为嵌套对象分配值 [英] how to assign values to nested object through json object array

查看:162
本文介绍了如何通过json对象数组为嵌套对象分配值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的模型课:

public class SearchForFlight
{
    public SearchForFlight()
    {
        Segments = new otherType();
    }
    public int AdultCount { get; set; }
    public JourneyType JourneyType { get; set; }
    public string Sources { get; set; }

    public otherType Segments { get; set; }
}
public class otherType
{
    public string Origin { get; set; }
    public string Destination { get; set; }
    public FlightCabinClass FlightCabinClass { get; set;}
    public DateTime PreferredDepartureTime { get; set;
    public DateTime PreferredArrivalTime { get; set; }
}

现在,我的要求是将对象和嵌套对象一起发布到外部api. 所需形式如下:

Now, My requirement is to post objects along with nested object to an external api. The required form is something like this:

{
   AdultCount: $("#AdultCount").val(),
   JourneyType: $("#JourneyType :selected").text(),
   PreferredAirlines: null,
   Segments: [
              {
               Origin: $("#Origin").val(),
               Destination: $("#Destination").val(),
               FlightCabinClass: $("#FlightCabinClass").val(),
               PreferredDepartureTime:$("#PreferredDepartureTime").val(),
               PreferredArrivalTime: $("#PreferredArrivalTime").val(),
              }
             ]
        }

因此,我创建了另一个类OtherType并将所有这些嵌套对象放入其中. 我从这个问题中得到了这个主意 如何使用以下方法将嵌套的json对象发送到mvc控制器阿贾克斯 现在,这是我的Script标记,其中包含所有代码,用于将简单对象与嵌套对象一起发布.但是嵌套对象的值显示为null. 我应该如何在此处对此代码建模.

So, i have created another class OtherType and put all those nested objects into it. I got the idea from this so question How to send nested json object to mvc controller using ajax Now, this is my Script tag with all the code inside to post simple objects along with nested objects.But nested objects value comes out to be null. How should i model this code here.

  <script>
                $(document).ready(function () {                 
                    $("#btnPost").click(function () {
                        var sof = {
                            AdultCount: $("#AdultCount").val(),
                            JourneyType: $("#JourneyType :selected").text(),
                            PreferredAirlines: null,
                                Segments: [
                                {
                                    Origin: $("#Origin").val(),
                                    Destination: $("#Destination").val(),
                                    FlightCabinClass: $("#FlightCabinClass").val(),
                                    PreferredDepartureTime: $("#PreferredDepartureTime").val(),
                                    PreferredArrivalTime: $("#PreferredArrivalTime").val(),
                                }
                            ],
                        };

                        $.ajax(
                            {
                                url: "/api/Flight/SearchFlight",
                                type: "Post",
                                data: sof,
                                success: function (data) {
                                    alert(data);
                                }
                            });        
                    });
                });
        </script>

出发地",目的地"的已发布属性"值显示为空.

Posted Properties values for Origin, Destination comes out to be null.

在视图页面上呈现的文本框如下所示:

The textbox rendered on view page are something like this:

 @Html.TextBoxFor(model => model.Segments.Origin)

请提示.

推荐答案

删除细分的数组[].使用contentType并在$ .ajax函数中进行字符串化.使用生成的ID作为起点.它可能不是来源".因此,请相应地对其进行更改.

Remove the array [] for Segments. Use contentType and stringify in your $.ajax func. Use the generated id for the Origin. It might not be "Origin". So,pls change it accordingly.

 <script>
            $(document).ready(function () {                 
                $("#btnPost").click(function () {
                    var sof = {
                        AdultCount: $("#AdultCount").val(),
                        JourneyType: $("#JourneyType :selected").text(),
                        PreferredAirlines: null,
                        Segments: {
                                Origin: $("#Origin").val(),
                                Destination: $("#Destination").val(),
                                FlightCabinClass: $("#FlightCabinClass").val(),
                                PreferredDepartureTime: $("#PreferredDepartureTime").val(),
                                PreferredArrivalTime: $("#PreferredArrivalTime").val(),
                            },
                    };

                    $.ajax(
                        {
                            url: "/api/Flight/SearchFlight",
                            type: "Post",
                            contentType: 'application/json',
                            data: JSON.stringify(sof),
                            success: function (data) {
                                alert(data);
                            }
                        });        
                });
            });
    </script>

这篇关于如何通过json对象数组为嵌套对象分配值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆