我有几个像下面这样的课程 -
public class P
{
public int Id { get; set; }
public virtual ICollection<PDetail> PDetails { get; set; }
}
public class PDetail
{
public int Id { get; set; }
public int Type { get; set; }
public double Price { get; set; }
}
现在,在我看来,我将其显示为 -
@foreach (var detail in Model.PDetails)
{
<div class="row">
<div class="col-sm-6">
@Html.DropDownListFor(m => detail.Type, (IEnumerable<SelectListItem>)ViewData["Types"], "--Type--", htmlAttributes: new { @class = "form-control" })
</div>
<div class="col-sm-6">
@Html.TextBoxFor(m => detail.Price, "", htmlAttributes: new { @class = "form-control", placeholder = "Price" })
</div>
</div>
}
在这里,我可以显示detail.Price
每个细节对象,但detail.Type
没有从ViewData["Types"]
下拉列表中选择。
PS:ViewData["Types"]
只是 typeIds = {1,2,3...} 的字典
信息1
我还尝试将视图更改为 -
@for (int i = 0; i < Model.PDetails.Count(); i++)
{
<div class="row">
<div class="col-sm-6">
@Html.DropDownListFor(m => m.PDetails.ElementAt(i).Type, (IEnumerable<SelectListItem>)ViewData["Types"], "--Type--", htmlAttributes: new { @class = "form-control" })
</div>
</div>
}
但它仍然无法正常工作。我该如何修复它?