Asp.net mvc 2 索引超出范围。必须是非负且小于集合大小的错误如何解决?

Asp.net mvc 2 索引超出范围。必须是非负且小于集合大小的错误如何解决?,asp.net-mvc-2,controller,Asp.net Mvc 2,Controller,嗨,我收到了这个错误: 我的代码是这样的,在Reservationcontroller中: private void loadCountry() { ReservationHelper reservationHelperObject = new ReservationHelper(); ViewData["countryOut"] = new SelectList(reservationHelperObject.LoadCountry());

嗨,我收到了这个错误:

我的代码是这样的,在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();
    }
private void loadDefaultvalues()
{
List emptyList=新列表();
ViewData[“locationIn”]=新的选择列表(emptyList);
ViewData[“locationOut”]=新的选择列表(emptyList);
ViewData[“RateProgram”]=新的选择列表(emptyList);
ViewData[“Currency”]=新的选择列表(emptyList);
ViewData[“VehicleClass”]=新的选择列表(清空列表);
ViewData[“ReservationStatusDropDownList”]=新的选择列表(emptyList);
ViewData[“Miles”]=新的选择列表(清空列表);
ViewData[“currencyDropDownList”]=新的选择列表(emptyList);
ViewData[“ReservationStatus”]=新建选择列表(emptyList);
ViewData[“vehicleClassDropDownList”]=新的选择列表(清空列表);
ViewData[“rateProgramDropDownList”]=新的选择列表(emptyList);
ViewData[“parentOutDropDownList”]=新的选择列表(emptyList);
ViewData[“parentInDropDownList”]=新的选择列表(emptyList);
ViewData[“zoneOutDropDownList”]=新的选择列表(emptyList);
ViewData[“zoneInDropDownList”]=新的选择列表(emptyList);
ViewData[“locationOutDropDownList”]=新的选择列表(emptyList);
ViewData[“locationInDropDownList”]=新的选择列表(emptyList);
ViewData[“authorizationDropDownList”]=新的选择列表(emptyList);
ViewData[“specialServiceDropDownList”]=新的选择列表(emptyList);
ViewData[“RentalStatus”]=新的选择列表(emptyList);
ViewData[“状态”]=新的选择列表(清空列表);
ViewData[“Fop”]=新建选择列表(清空列表);
ViewData[“CustomerType”]=新的选择列表(emptyList);
this.loadRentaStatus();
这是loadStatus();
这是loadFop();
this.loadCustomerTypes();
this.loadCountry();
this.loadReservationStatus();
//此.loadMilesPercentage()
这个.loadSpecialService();
这个.loadRAType();
这是loadAirline();
这是loadTerminal();
这是loadParentCode();
这个.loadZoneCode();
此.loadAuthorizationType();
}

谢谢

此代码的计算结果为空列表:

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

此代码的计算结果为空列表:

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