你可以有你的视图模型是这样的:
public class LicenseViewModel
{
public IEnumerable<SelectListItem> LicensedState { get; private set; }
public IEnumerable<SelectListItem> LicenseType { get; private set; }
public LicenseViewModel(string licensedState = null, string licenseType = null)
{
LicensedState = LicensedStatesProvider.All().Select(s=> new SelectListItem
{Selected = licensedState!= null && s == licensedState, Text = s, Value = s});
LicenseType = LicenseTypesProvider.All().Select(t => new SelectListItem
{ Selected = licenseType != null && t == licenseType, Text = t, Value = t });
}
}
LicensedStatesProvider
和LicenseTypesProvider
只是让所有LicensedStates和LicenseTypes的方式,就看你如何得到它们。
,并考虑,你有这样的事情:
@foreach (var license in Model.Licenses)
{
//other stuff...
@Html.DropDownList("LicensedState", license.LicensedState)
@Html.DropDownList("LicenseType", license.LicenseType)
}