如何通过json对象数组为嵌套对象赋值

时间:2021-12-09 15:56:44

This is my model class:

这是我的模型类:

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; }
}

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

现在,我的要求是将对象和嵌套对象一起发布到外部api。所需的表格是这样的:

{
   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(),
              }
             ]
        }

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.

所以,我创建了另一个类OtherType并将所有嵌套对象放入其中。我从这个问题得到了这个问题如何使用ajax将嵌套的json对象发送到mvc控制器现在,这是我的脚本标记,里面的所有代码都可以发布简单对象和嵌套对象。但是嵌套对象的值是null。我应该如何在这里建模这个代码。

  <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.

发布的Origin属性值,Destination出来是null。

The textbox rendered on view page are something like this:

在视图页面上呈现的文本框是这样的:

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

Any hint please.

请提示任何提示。

1 个解决方案

#1


1  

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.

删除段的数组[]。在$ .ajax func中使用contentType和stringify。使用生成的Origin作为Origin。它可能不是“起源”。所以,请相应地改变它。

 <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>

#1


1  

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.

删除段的数组[]。在$ .ajax func中使用contentType和stringify。使用生成的Origin作为Origin。它可能不是“起源”。所以,请相应地改变它。

 <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>