[英]What is the use of the first parameter in HTML.DropDownListFor?
我正在阅读HTML.DropDownListFor上的文档,它指出以下内容:
public static MvcHtmlString DropDownListFor<TModel, TProperty>(
this HtmlHelper<TModel> htmlHelper,
Expression<Func<TModel, TProperty>> expression,
IEnumerable<SelectListItem> selectList
)
该表达式有什么作用? 我一直在阅读,并说将其绑定到属性?
我使用了以下代码:
@(Html.DropDownListFor(x => x.SiteList[0], new SelectList(Model.SiteList, "LocationID", "Description",Model.SiteList[5].LocationID)))
该代码的工作原理如下:
@(Html.DropDownListFor(x => x.SiteList[0].LocationID, new SelectList(Model.SiteList, "LocationID", "Description",Model.SiteList[5].LocationID)))
和
@(Html.DropDownListFor(x => x.SiteList[0].Description, new SelectList(Model.SiteList, "LocationID", "Description",Model.SiteList[5].LocationID)))
SiteList是:
List<Site>
网站为:
public class Site {
public string LocationID;
public string Description;
}
我不明白示例中lambda的目的是什么以及如何在输出中使用它?
因此,通常在列表旁边,您的模型上会为此拥有一个单独的属性。 当从列表中选择一个值时,它将设置绑定到的属性。
class ListType{
public string Key { get; set; }
public string Value { get; set; }
}
class ViewModel{
public IEnumerable<SelectListItem> List { get; set; } //This is generated from a list of ListType objects
public string Selected { get; set; }
}
然后,您可以使用:
@Html.DropDownListFor(x => x.Selected, Model.List)
这意味着当表单回发时,那么Selected属性将在列表中具有所选项目的值。
请参见由三个表达式生成的html。 在所有情况下,名称和ID都将不同。 这些名称是在将所选值绑定到模型的属性时使用的。元素的名称应与发布页面时将所选值投标的属性的名称相同。为元素命名以达到目的的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.