0

AspMVC 5. My select lists work fine when it is just one ddl in a form. But if I have a list of DDLs, they don't display the value (ApprovalId) from the controller. The select list values are populated correctly in both scenarios, but in the loop, the approval Id is always the default value ('1').

SearchResultsModel:

public class SearchResultModel
{
    public int ApprovalId { get; set; }
}

View Model:

    public List<SearchResultModel> SearchResults { get; set; }
    public IEnumerable<SelectListItem> ApprovalSelectList { get; set; }

Controller:

    public ActionResult SearchResults(int employeeId)
    {
        SearchResourceModel model = new SearchResourceModel();
        model.SearchResults = repo.GetFilteredResources(employeeId);
        model.ApprovalSelectList = repo.GetApprovalTypes();
        return PartialView("_ResourcesResults", model);
    }

Populating the select list:

    public IEnumerable<SelectListItem> GetApprovalTypes()
    {
        using (var db = new FireResourceEntities())
        {
            return db.ApprovalType.ToList().Select(d => new SelectListItem()
            {
                Text = d.ApprovalDescription,
                Value = d.ApprovalTypeId.ToString()
            });
        }
    }

So a regular one that works:

@Html.DropDownListFor(m => m.SearchResults.ApprovalId, Model.ApprovalSelectList)

But if they are in a list, the default value is displayed:

    for (int i = 0; i < Model.SearchResults.Count; i++)
    {
        <tr>
            <td>@Html.DropDownListFor(m => m.SearchResults[i].ApprovalId, Model.ApprovalSelectList)</td>
        </tr>
    }

I verified the correct values are returning from the controller. Why is it different when displayed in the loop?

BattlFrog
  • 3,370
  • 8
  • 56
  • 86

0 Answers0