Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在ASP.NETMVC中创建自定义HTML帮助程序方法DropDownFor_C#_Asp.net Mvc_Lambda_Html Helper_Expression Trees - Fatal编程技术网

C# 如何在ASP.NETMVC中创建自定义HTML帮助程序方法DropDownFor

C# 如何在ASP.NETMVC中创建自定义HTML帮助程序方法DropDownFor,c#,asp.net-mvc,lambda,html-helper,expression-trees,C#,Asp.net Mvc,Lambda,Html Helper,Expression Trees,我正在尝试创建一个下拉框,当用户无法访问标签时,该下拉框将在特定条件下呈现标签 到目前为止,我已经想出了这个 public static MvcHtmlString ReadOnlyCapableDropDownFor<TModel, TProperty>( this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expressi

我正在尝试创建一个下拉框,当用户无法访问标签时,该下拉框将在特定条件下呈现标签

到目前为止,我已经想出了这个

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);
}
public static MvcHtmlString ReadOnlyCapableDropDownFor(
这个HtmlHelper HtmlHelper,
表情表情,
IEnumerable selectList,
bool启用,
对象属性
)
{
return!enabled?htmlHelper.DisplayFor(表达式)
:htmlHelper.DropDownListFor(表达式、选择列表、htmlAttributes);
}
如果启用,则正确呈现标签;如果启用,则正确呈现下拉列表;如果启用,则正确呈现下拉列表。问题在于,标签的文本是所选选择列表值的id,而不是通常在下拉列表中显示的文本


这很有意义,因为我正在使用表达式作为显示的值,如何使用此表达式获取选择列表项文本值而不是数据值?

您需要自己查找文本值。您应该能够在此例程中执行此操作,因为您有可用的选择列表:

? htmlHelper.DisplayFor(selectList.Single(x=>x.Value == expression).Text

尽管在上述代码中使用表达式之前,您可能需要对其求值。

您可以编译表达式并从模型中检索值。然后,从选择列表中选择正确的文本

TProperty val=expression.Compile().Invoke(htmlhelp.ViewData.Model);
SelectListItem selectedItem=selectList.Where(item=>item.Value==Convert.ToString(val)).FirstOrDefault();
字符串文本=”;
如果(selectedItem!=null)text=selectedItem.text;
回来!启用?新的MvcHtmlString(“+text+”)
:htmlHelper.DropDownListFor(表达式、选择列表、htmlAttributes);

我认为在这种情况下,返回一个特别的MvcHtmlString就足够了,因为您拥有的所有信息都包含在selectList字符串中。(您的helper方法不能访问任何数据注释等。)

这不起作用,因为表达式是表达式类型,而不是字符串。您必须以某种方式解析表达式。ExpressionHelper.GetExpressionText似乎将解析为字符串,但仍然无法将其解析为字符串compile@dbaseman他有正确的想法。您不需要DisplayFor,只需生成一个span并返回文本即可。这很有效,MvcHtmlString有一个小错误,必须使用。Create和我还将where更改为FirstOrDefault(),否则一切都好,干杯
TProperty val = expression.Compile().Invoke(htmlHelper.ViewData.Model);
SelectListItem selectedItem = selectList.Where(item => item.Value == Convert.ToString(val)).FirstOrDefault();
string text = "";
if (selectedItem != null) text = selectedItem.Text;

return !enabled ? new MvcHtmlString("<span>" + text + "</span>")
      : htmlHelper.DropDownListFor(expression, selectList, htmlAttributes);