如何访问LabelFor、EditorFor、ValidationMessageFor等html扩展...For
我正在编写自己的扩展,就像这样
Imports System
Imports System.Web.Mvc
Imports System.Web.Mvc.Html
Imports System.Web
Imports System.Text
Public Module HtmlExtensions
<System.Runtime.CompilerServices.Extension()> _
Public Function Asistente(Of TModel As Class)(ByVal helper As HtmlHelper, model As TModel) As MvcHtmlString
helper.ValidationMessage("Home") 'It works fine
helper.ValidationMessagefor() 'It show the next message 'ValidationMessagefor' is not a member of 'System.Web.Mvc.HtmlHelper
End Function
...实际上,这是因为我想生成一个这样的mvcHtmlString
<div class="editor-label">
@Html.LabelFor(Function(model) model.Numero)
</div>
<div class="editor-field">
@Html.EditorFor(Function(model) model.Numero)
**@Html.ValidationMessageFor(Function(model) model.Numero)**
</div>提前致以问候
发布于 2011-05-11 04:53:52
ValidationMessageFor方法需要泛型类型html帮助器。所以你的方法应该看起来像这样:
Public Shared Function Asistente(Of TModel, TProperty)(helper As HtmlHelper(Of TModel), expression As Expression(Of Func(Of TModel, TProperty))) As HtmlString
Return helper.ValidationMessageFor(expression)
End Function这将允许您对任何型号使用该扩展,并且您的呼叫不会改变。不过,我不太清楚你在问什么。
**@Html.Asistente(Function(model) model.Numero)**(请注意,vb是使用c#到vb的转换器生成的-但它看起来是正确的)
如果你想使用你发布的第一个样本,那么你必须像这样写它:
Public Shared Function Test(Of T As User)(helper As HtmlHelper(Of T), model As T) As HtmlString
Return helper.ValidationMessageFor(Function(f) f.Name)
End Function但是,这是一个特定的用例,因为如果不强制将模型类型设置为特定类型,则不会显示lambda表达式属性,因此不会进行编译。
https://stackoverflow.com/questions/5954609
复制相似问题