无法将KeyValuePair传递给控制器​​,值为null

时间:2014-08-11 11:16:51

标签: c# asp.net-mvc-3

我尝试传递一个对象,因为View作为ViewModel查看控制器。但有些到达控制器的空场。这些字段是KeyValuePair,并且无法将其正确传递给Controller。

这是我的代码:

控制器:

public ActionResult Index(SearchResultsViewModel searchResultsViewModel)
        {

            PassengerDataViewModel viewModel = new PassengerDataViewModel()
            {
                Request = searchResultsViewModel,
                PassengerType = GetPassengerTypes(),
                NumberPeople = GetNumberPeople()

            };
            return View("PassengerData", viewModel);
        }

视图模型:

public class SearchResultsViewModel
{

    public SearchViewModel Request { get; set; }

    public string SearchFlightDatesText { get; set; }

    public string SearchPaxSelectionText { get; set; }


    public KeyValuePair<string, string> DepartureStation { get; set; }

    public KeyValuePair<string, string> ArrivalStation { get; set; }


    public DateTime BeginDate { get; set; }

    public DateTime EndDate { get; set; }

    public DateTime SelectedDate { get; set; }

    public int SelectedDay { get; set; }

    public List<FlightsResponseDTO> Days { get; set; }



    public KeyValuePair<string, string> DepartureStation2 { get; set; }

    public KeyValuePair<string, string> ArrivalStation2 { get; set; }


    public List<FlightsResponseDTO> Days2 { get; set; }

    public DateTime BeginDate2 { get; set; }

    public DateTime EndDate2 { get; set; }

    public DateTime SelectedDate2 { get; set; }

    public int SelectedDay2 { get; set; }



}

查看代码

    Post = function (path, params, method) {

                method = method || "post"; // Set method to post by default if not specified.

                // The rest of this code assumes you are not using a library.
                // It can be made less wordy if you use one.
                var form = document.createElement("form");
                form.setAttribute("method", method);
                form.setAttribute("action", path);
                debugger;
                for (var key in params) {
                    if (params.hasOwnProperty(key)) {
                        var hiddenField = document.createElement("input");
                        hiddenField.setAttribute("type", "hidden");
                        hiddenField.setAttribute("name", key);
                        hiddenField.setAttribute("value", params[key]);

                        form.appendChild(hiddenField);
                    }
                }

                debugger;
                document.body.appendChild(form);
                form.submit();
            }
  $("a.baseButton.yellowButton.mediumButton").click(function (event) {


            //Comprobamos si esta seleccionado un vuelo
            if(!checkIfFlightSelected()){
                $('#error-dialog').dialog({
                    modal: true,
                    height: 130,
                    width: 360
                });                         
                event.preventDefault();
                return false;
            }



            //--------------Inicio SearchViewModel ------------------------------
            debugger;
            var departureStation = "@Model.Request.SelectedOrigin1";
            var arrivalStation = "@Model.Request.SelectedDestination1";
            var departureDate = "@Model.Request.SelectedFlightDate1";
            var departureStation2 = "@Model.Request.SelectedOrigin2";
            var arrivalStation2 = "@Model.Request.SelectedDestination2";
            var departureDate2 = "@Model.Request.SelectedFlightDate2";
            var adults = "@Model.Request.NumAdults";
            var childs = "@Model.Request.NumChilds";
            var infants = "@Model.Request.NumInfants";
            var residentOrFamily ="@Model.Request.SelectedResidentOrLargueFamilyDiscount";
            var PassengersWithDiscount = "@Model.Request.NumPassengersWithSpecialDiscount";
            var tipo = "@Model.Request.SelectedSearchType";

            var searchViewModelRequest = {
                SelectedSearchType: tipo,
                SelectedOrigin1: departureStation,
                SelectedDestination1: arrivalStation,
                SelectedFlightDate1: departureDate,
                Tipo:tipo,
                //SelectedOrigin2: departureStation2,
                //SelectedDestination2: arrivalStation2,
                //SelectedFlightDate2: departureDate2,

                NumAdults: adults,
                NumChilds: childs,
                NumInfants: infants,
                SelectedResidentOrLargueFamilyDiscount: residentOrFamily,
                NumPassengersWithSpecialDiscount: PassengersWithDiscount
            };
            //-------------------Fin SearchViewModel---------------------------------



            //Inicio SearchResultsViewModel

            //-----------Datos para obtenr datos seleccioandos------------------

            var partsId = $("#availabilityTable0 :input:checked")[0].id.split('_');
            var day = calendariCarregat.Days[partsId[1]];
            var journey=day.Journeys[partsId[2]];           
            var departureDate = new Date(journey.JourneySellKey.split('~')[5].split(' ')[0]);
            //var arrivalDate = new Date(journey.JourneySellKey.split('~')[7].split(' ')[0]);    
            var departureHourDate = journey.DepartureTimeString;
            var arrivalHourDate = journey.ArrivalTimeString;            

            var departureDay;
            var departureMonth;

            if (departureDate.getUTCDate() + 1 < 10) {
                departureDay = "0" + (departureDate.getUTCDate() + 1);
            } else {
                departureDay = (departureDate.getUTCDate() + 1);
            }
            if (departureDate.getUTCDate() + 1 < 10) {
                departureMonth = "0" + (departureDate.getUTCMonth() + 1);
            } else {
                departureMonth = (departureDate.getUTCMonth() + 1);
            }


            var normalDepartureDate = (departureDay) + "/" + (departureMonth) + "/" + departureDate.getUTCFullYear();

            //----------------------Fin datos seleccionados--------------

            var searchFlightDatesText = "@Model.SearchFlightDatesText"; 
            var searchPaxSelectionText = "@Model.SearchPaxSelectionText";
            var departureStation = {
                "Key": "@Model.DepartureStation.Key",
                "Value": "@Model.DepartureStation.Value"
            };
            var arrivalStation = {
                "Key": "@Model.ArrivalStation.Key",
                "Value": "@Model.ArrivalStation.Value"
            };
            var beginDate = "@Model.BeginDate";
            var endDate = "@Model.EndDate";
            var selectedDate = normalDepartureDate;
            var selectedDay = "@Model.SelectedDay";
            var days = "@Model.Days";

            //var departureStation2 = null;
            //var arrivalStation2 = null;
            //var days2 = null;
            //var beginDate2 = null;
            //var endDate2 = null;
            //var selectedDate2 = null;
            //var selectedDay2 = null;




            var searchResultsViewModel = {

                //Request: searchViewModelRequest.toString(),
                "Request.SelectedOrigin1": searchViewModelRequest.SelectedOrigin1,
                "Request.SelectedSearchType": searchViewModelRequest.Tipo,            
                "Request.SelectedDestination1": searchViewModelRequest.SelectedDestination1,
                "Request.SelectedFlightDate1": searchViewModelRequest.SelectedFlightDate1,
                "Request.NumAdults": searchViewModelRequest.NumAdults,
                "Request.NumChilds": searchViewModelRequest.NumChilds,
                "Request.NumInfants": searchViewModelRequest.NumInfants,
                "Request.IncrementSelectedFlightDate1": 0,
                "Request.SelectedResidentOrLargueFamilyDiscount": searchViewModelRequest.SelectedResidentOrLargueFamilyDiscount,
                "Request.NumPassengersWithSpecialDiscount": searchViewModelRequest.NumPassengersWithSpecialDiscount,

                SearchFlightDatesText: searchFlightDatesText,
                SearchPaxSelectionText: searchPaxSelectionText,
                "DepartureStation.Key": "test",
                "DepartureStation.Value": "test",

                "ArrivalStation": "{ [test, test]}",

                BeginDate: beginDate,
                EndDate: endDate,
                SelectedDate: selectedDate,
                SelectedDay: selectedDay,
                Days: days,

                "DepartureStation2.Key": "test",
                "DepartureStation2.Value":  "test",


            };
           //--------------Fin SearchResultsViewModel


            MICESearcher.Post('@Url.Action("Index", "PassengerData")', (searchResultsViewModel));

    });

我已经尝试过将多种方式的值传递给KeyValuePair,但我无法实现它

编辑(解决):

最后感谢@Daniel J.G,我可以通过关注此网站解决问题ASP MVC.NET - how to bind KeyValuePair?

谢谢大家的帮助!!

0 个答案:

没有答案