ListBoxFor 的 MVC Razor 二级模型
MVC Razor two level Model for ListBoxFor
视图中是否有一种方法可以向下钻取一层以获取 GenericValues 中的数据对象?我知道我可以更改模型以使其工作,但我不想添加额外的模型 类?
查看
<div class="form-group row">
@Html.LabelFor(model => model.selectedLanguage)
@Html.ListBoxFor(
x => x.selectedLanguage,
new SelectList(Model.ListOfLanguages, "Value.Value", "Value.Name"),
new { id = "multiselectLanguage", @class = "form-control" })
</div>
型号
public List<LanguageModel> ListOfLanguages { get; set; }
public class LanguageModel
{
public string ISOCode { get; set; }
public GenericValues Values { get; set; }
}
public class GenericValues
{
public string Name { get; set; }
public int Value { get; set; }
}
你可以用linq得到list<GenericValues>
:
@Html.ListBoxFor(
x => x.selectedLanguage,
new SelectList(Model.ListOfLanguages.Select(x => x.Values).Distinct(), "Value", "Name"),
new { id = "multiselectLanguage", @class = "form-control" })
视图中是否有一种方法可以向下钻取一层以获取 GenericValues 中的数据对象?我知道我可以更改模型以使其工作,但我不想添加额外的模型 类?
查看
<div class="form-group row">
@Html.LabelFor(model => model.selectedLanguage)
@Html.ListBoxFor(
x => x.selectedLanguage,
new SelectList(Model.ListOfLanguages, "Value.Value", "Value.Name"),
new { id = "multiselectLanguage", @class = "form-control" })
</div>
型号
public List<LanguageModel> ListOfLanguages { get; set; }
public class LanguageModel
{
public string ISOCode { get; set; }
public GenericValues Values { get; set; }
}
public class GenericValues
{
public string Name { get; set; }
public int Value { get; set; }
}
你可以用linq得到list<GenericValues>
:
@Html.ListBoxFor(
x => x.selectedLanguage,
new SelectList(Model.ListOfLanguages.Select(x => x.Values).Distinct(), "Value", "Name"),
new { id = "multiselectLanguage", @class = "form-control" })