Creating custom Html Helper: MyHelperFor

前端 未结 4 1776
北海茫月
北海茫月 2020-12-14 04:40

I would like to create a helper that can be used like

@Html.MyHelperFor(m => m.Name)

this should return for example

4条回答
  •  眼角桃花
    2020-12-14 05:02

    This should get you started. This function directly returns the property name but you should be able to convert this into the extension you are looking for with a little work. This example has the correct method signature and the call to ExpressionHelper to get the name of your property.

        public static MvcHtmlString MyHelperFor(this HtmlHelper htmlHelper, Expression> expression)
        {
            string expressionName = ExpressionHelper.GetExpressionText(expression);
    
            return new MvcHtmlString(expressionName);
        }
    

提交回复
热议问题