2011-05-04 28 views
1

你好我得到這個錯誤:指數超出範圍。必須爲非負值且小於集合的大小錯誤如何解決?

enter image description here

我的代碼是這樣的,在Reservationcontroller

private void loadCountry() 
    { 
     ReservationHelper reservationHelperObject = new ReservationHelper(); 

     ViewData["countryOut"] = new SelectList(reservationHelperObject.LoadCountry()); 
     ViewData["countryIn"] = new SelectList(reservationHelperObject.LoadCountry()); 

     CountryOut = ((MultiSelectList)(ViewData["CountryOut"])).ToList()[0].Text; 
    } 

任何想法?

延續的代碼:

private void loadDefaultvalues() 
    { 
     List<string> emptyList = new List<string>(); 
     ViewData["locationIn"] = new SelectList(emptyList); 
     ViewData["locationOut"] = new SelectList(emptyList); 
     ViewData["RateProgram"] = new SelectList(emptyList); 
     ViewData["Currency"] = new SelectList(emptyList); 
     ViewData["VehicleClass"] = new SelectList(emptyList); 
     ViewData["ReservationStatusDropDownList"] = new SelectList(emptyList); 
     ViewData["Miles"] = new SelectList(emptyList); 
     ViewData["currencyDropDownList"] = new SelectList(emptyList); 
     ViewData["ReservationStatus"] = new SelectList(emptyList); 
     ViewData["vehicleClassDropDownList"] = new SelectList(emptyList); 
     ViewData["rateProgramDropDownList"] = new SelectList(emptyList); 
     ViewData["parentOutDropDownList"] = new SelectList(emptyList); 
     ViewData["parentInDropDownList"] = new SelectList(emptyList); 
     ViewData["zoneOutDropDownList"] = new SelectList(emptyList); 
     ViewData["zoneInDropDownList"] = new SelectList(emptyList); 
     ViewData["locationOutDropDownList"] = new SelectList(emptyList); 
     ViewData["locationInDropDownList"] = new SelectList(emptyList); 
     ViewData["authorizationDropDownList"] = new SelectList(emptyList); 
     ViewData["specialServiceDropDownList"] = new SelectList(emptyList); 

     ViewData["RentalStatus"] = new SelectList(emptyList); 
     ViewData["Status"] = new SelectList(emptyList); 
     ViewData["Fop"] = new SelectList(emptyList); 
     ViewData["CustomerType"] = new SelectList(emptyList); 

     this.loadRentaStatus(); 
     this.loadStatus(); 
     this.loadFop(); 
     this.loadCustomerTypes(); 
     this.loadCountry(); 
     this.loadReservationStatuses(); 
     //this.loadMilesPercentage() 
     this.loadSpecialService(); 
     this.loadRAType(); 
     this.loadAirline(); 
     this.loadTerminal(); 
     this.loadParentCode(); 
     this.loadZoneCode(); 
     this.loadAuthorizationType(); 
    } 

感謝

回答

1

此代碼評估空列表:

((MultiSelectList)(ViewData["CountryOut"])).ToList() 

如何解決呢?最好的辦法是做像下面這樣:

private void loadCountry() 
{ 
    ReservationHelper reservationHelperObject = new ReservationHelper(); 

    ViewData["countryOut"] = new SelectList(reservationHelperObject.LoadCountry()); 
    ViewData["countryIn"] = new SelectList(reservationHelperObject.LoadCountry()); 

    var list = ((MultiSelectList)(ViewData["CountryOut"])).ToList(); 
    if (list.Count > 0) { 
     CountryOut = list[0].Text; 
    } else { 
     // do something sensible when you have no data 
    } 
} 
相關問題