【发布时间】:2012-06-26 03:01:03
【问题描述】:
我正在尝试创建一个下拉框,当用户无权访问它时,它将在某些条件下呈现标签。
到目前为止,我想出了这个
public static MvcHtmlString ReadOnlyCapableDropDownFor<TModel, TProperty>(
this HtmlHelper<TModel> htmlHelper,
Expression<Func<TModel, TProperty>> expression,
IEnumerable<SelectListItem> selectList,
bool enabled,
object htmlAttributes
)
{
return !enabled ? htmlHelper.DisplayFor(expression)
: htmlHelper.DropDownListFor(expression, selectList, htmlAttributes);
}
当启用为 false 时正确呈现标签,当它为 true 时呈现下拉列表,问题是标签的文本是所选选择列表值的 id,而不是通常显示在下拉列表中的文本.
这是有道理的,因为我将表达式用于显示的值,我如何使用该表达式来获取选择列表项文本值而不是数据值?
【问题讨论】:
标签: c# asp.net-mvc lambda html-helper expression-trees