简体   繁体   中英

Drop down list not populating from List MVC

I am confused on an issue with the DropDownListFor helper.

I am trying to assign a list of values to the drop down as follows:

Model

public Int32 TestID {get;set;}

public List<Int32> ListOfID {get;set;}

public IEnumerable<SelectListItem> TimeDD {get;set;}

Controller

public ActionResult Manage(int id){

    MyModel model = new MyModel();

    model.TimeDD = DropDownManager.TimeDD;

    model.TestID = 12;
    model.ListOfID = new List<Int32>{ 1,2,3,4,5,6,7};
    return View(model);
}

In the view I have the following:

@for(int i = 0; i< ListoFID.Count; i++){

     <div>@Html.DropDownListFor(m=> m.TestID, Model.TimeDD)</div>
     <div>@Html.DropDownListFor(m=> m.ListoFID[i], Model.TimeDD)</div>
}

The problem I am having is that the TestID drop down is working correctly however the ListoFID[i] is not selecting the values from the drop down. The TimeDD is a list of times as follows:

/// <summary>
        /// The Time dropdown
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        public static IEnumerable<SelectListItem> TimeDD()
        {
            // new ctl
            TimeControl ctl = new TimeControl();

            // get drop down
            IEnumerable<SelectListItem> result = ctl.Select().Select(m => new SelectListItem { Text = m.Time1, Value = m.TimeID.ToString() }).ToList();

            // clean up
            ctl.Dispose();

            return result;
        }

Where Time1 is '09:30' and TimeID is 1 - 48. I cannot figure out how this is happening!! As soon as I reference an object it fails to select the drop down at the correct point.

Edit Also I have a property in my model called OpeningTimes - this is the list of opening times saved against a company as below:

ComapnyID    DayID    StartTime    EndTime
   1           1         19           32        -- e.g. Monday 09:00 - 17:30

When I loop through the Opening times:

@for(int i=0; i< Model.OpeningTimes.Count; i++)
        {
            <tr>

                <td>
                   @Html.DropDownListFor(m => m.OpeningTimes[i].StartTime, Model.TimeDD)
                </td>
                <td>
                    @Html.DropDownListFor(m => m.OpeningTimes[i].EndTime, Model.TimeDD)
                </td>

            </tr>
        }

The drop downs are still not being selected. I can confirm the StartTime and EndTime do have values and are property int

DropDownListFor requires a property to map to, not a value. So

@Html.DropDownListFor(m=> m.ListoFID[i], Model.TimeDD) /* the [i] kills it */

will not work.

instead of doing a list of int build it like the other

List<SelectListItem> ls = new List<SelectListItem>();
foreach(var temp in Model.ListOfId){
    ls.Add(new SelectListItem() { Text = temp, Value = temp});
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM