我有以下 Razor 语法的视图:
@model DoggrOPI.Models.WellSearch
<div class="container-fluid">
<h2>Well Search</h2>
@for (int i = 0; i < Model.Counties.Count; i++)
{
@Html.DisplayFor(m => m.Counties[i].CountyName)
}
</div>
显示:
Value 1Value 2Value 3
这正是我想要的,但我无法将其绑定到@Html.DropDownListFor。这样做的正确语法是什么?
我的模型是这样的:
public partial class CountyInfo
{
public string CountyName { get; set; }
}
型号:
public class WellSearch
{
public int CountyId { get; set; }
public IEnumerable<CountyInfo> Counties { get; set; }
}
public partial class CountyInfo
{
public int CountyId {get; set;}
public string CountyName { get; set; }
}
查看:
@Html.DropDownListFor(m => m.CountyId , new SelectList(Model.Counties), "CountyId", "CountyName "))
基本上将
CountyId
添加到您的主视图模型中,当您将表单发送到服务器时,它将与您选择的下拉选项相匹配。