2

im trying pass a object since View to controller as ViewModel. But some arrive field empty to controller. This fields are KeyValuePair and have not idea for pass it correctly to Controller.

This is my code:

Controller:

public ActionResult Index(SearchResultsViewModel searchResultsViewModel)
        {

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

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

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



}

View Code

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

    });

i have tried it pass values of many ways to the KeyValuePair but i could not achieve it

Edit(solved):

finally thanks to @Daniel J.G, I could solve the problem by following this website ASP MVC.NET - how to bind KeyValuePair?

thank you all for the help!!

Community
  • 1
  • 1
Miguel Remirez
  • 363
  • 1
  • 11
  • Maybe it is because the generic KeyValuePair is not serializable ? – S.L. Aug 11 '14 at 11:19
  • 1
    Wall of text isn't the best way to express your problem. You may consider to use [minimal complete verifiable example] instead (http://stackoverflow.com/help/mcve). It will describe your problem more precisely, remove noise and reduce any further speculations about actual details. And even more - creating clean and short problem demo will improve your own understanding of both the issue and the whole program. – Eugene Podskal Aug 11 '14 at 11:25
  • and if so, what i can do? – Miguel Remirez Aug 11 '14 at 11:26
  • 1
    Use custom classes for KeyValue, you should try this to find out if it is the serialization problem – S.L. Aug 11 '14 at 11:27
  • 2
    I think you will need to use your own class for it, as `KeyValue` doesn't have a parameterless constructor nor public setters for the `Key` and `Value` properties. Either that or create your own model binder. See [this](http://stackoverflow.com/questions/8538846/asp-net-mvc-3-binding-user-control-of-type-keyvaluepair-to-viewmodel) and [this](http://stackoverflow.com/questions/1300642/asp-mvc-net-how-to-bind-keyvaluepair) – Daniel J.G. Aug 11 '14 at 11:41

0 Answers0