AspMVC 5.我的选择列表在表单中只有一个ddl时工作正常。但是,如果我有一个DDL列表,它们不会显示控制器的值(ApprovalId)。在两种情况下都会正确填充选择列表值,但在循环中,批准ID始终是默认值(“1”)。
SearchResultsModel:
public class SearchResultModel
{
public int ApprovalId { get; set; }
}
查看型号:
public List<SearchResultModel> SearchResults { get; set; }
public IEnumerable<SelectListItem> ApprovalSelectList { get; set; }
控制器:
public ActionResult SearchResults(int employeeId)
{
SearchResourceModel model = new SearchResourceModel();
model.SearchResults = repo.GetFilteredResources(employeeId);
model.ApprovalSelectList = repo.GetApprovalTypes();
return PartialView("_ResourcesResults", model);
}
填充选择列表:
public IEnumerable<SelectListItem> GetApprovalTypes()
{
using (var db = new FireResourceEntities())
{
return db.ApprovalType.ToList().Select(d => new SelectListItem()
{
Text = d.ApprovalDescription,
Value = d.ApprovalTypeId.ToString()
});
}
}
这是一个有效的常规方法:
@Html.DropDownListFor(m => m.SearchResults.ApprovalId, Model.ApprovalSelectList)
但如果它们在列表中,则会显示默认值:
for (int i = 0; i < Model.SearchResults.Count; i++)
{
<tr>
<td>@Html.DropDownListFor(m => m.SearchResults[i].ApprovalId, Model.ApprovalSelectList)</td>
</tr>
}
我验证了从控制器返回的正确值。为什么在循环中显示时会有所不同?