我怎样才能在一个扩展方法使用HTML辅助方法? [英] How can I use a HTML helper method in an extension method?

查看:89
本文介绍了我怎样才能在一个扩展方法使用HTML辅助方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下类:

 公共类注意事项
{
    公共字符串文本{搞定;组; }
    公共RowInfo RowInfo {搞定;组; }
}公共类RowInfo
{
    [显示名称(创建)]
    公众的DateTime创建{搞定;组; }
    [显示名称(修改时间)
    公众的DateTime修改{搞定;组; }
}

在我看来,我有一个用正确的名称和值创建HTML如下:

  Html.HiddenFor(型号=> model.Note.Created)

现在我所要做的就是创建一个扩展方法,将包括上述那我可以在每个视图调用。我曾尝试做以下。我觉得我是在正确的轨道上,但我不知道该怎么做,相当于模式=> model.Note.Created 有人可以给我一些建议我如何能做到这一点,我会需要更换与括号内的文字。我没有一个模型,但我能做到一些其他的方式,以便隐藏字段会去看看我的类来获得正确的显示名称,就像上面呢?

 命名空间ST.WebUx.Helpers.Html
   {
    使用System.Web.Mvc;
    使用System.Web.Mvc.Html
    使用System.Linq的;公共静态类StatusExtensions
{
    公共静态MvcHtmlString StatusBox(此的HtmlHelper帮手,RowInfo RowInfo)
    {
        返回新MvcHtmlString(
           这里有些东西......+
           System.Web.Mvc.Html.InputExtensions.Hidden(用于创建场)+
           System.Web.Mvc.Html.InputExtensions.Hidden(改性场));
    }}


解决方案

您可以写一个强类型的辅助服用λ-EX pression:

 公共静态类StatusExtensions
{
    公共静态IHtmlString StatusBox<的TModel,TProperty>(
        这与的HtmlHelper LT;的TModel>帮手,
        防爆pression<&Func键LT;的TModel,TProperty>>恩
    )
    {
        返回新HtmlString(
           这里有些东西......+
           helper.HiddenFor(前));
    }
}

和则:

  @ Html.StatusBox(型号=> model.RowInfo.Created)


更新:

按照要求在评论部分,这里的帮手的修订版:

 公共静态类StatusExtensions
{
    公共静态IHtmlString StatusBox<&的TModel GT;(
        这与的HtmlHelper LT;的TModel>帮手,
        防爆pression<&Func键LT;的TModel,RowInfo>>恩
    )
    {
        VAR createdEx =
            防爆pression.Lambda<&Func键LT;的TModel,日期和GT;>(
                防爆pression.Property(ex.Body,创建),
                ex.Parameters
            );
        VAR modifiedEx =
            防爆pression.Lambda<&Func键LT;的TModel,日期和GT;>(
                防爆pression.Property(ex.Body,修改时间),
                ex.Parameters
            );        返回新HtmlString(
            这里有些东西......+
            helper.HiddenFor(createdEx)+
            helper.HiddenFor(modifiedEx)
        );
    }
}

和则:

  @ Html.StatusBox(型号=> model.RowInfo)

不用说,定制HTML辅助应该用来生成HTML的一小部分。复杂性可能迅速增长,在这种情况下我会使用为 RowInfo 类型的编辑模板,建议您。

I have the following classes:

public class Note
{
    public string Text { get; set; }
    public RowInfo RowInfo { get; set; }
}

public class RowInfo
{
    [DisplayName("Created")]
    public DateTime Created { get; set; }
    [DisplayName("Modified")]
    public DateTime Modified { get; set; }
}

In my view I have the following which creates HTML with the correct name and value:

Html.HiddenFor(model => model.Note.Created)

Now what I am trying to do is to create an extension method that will include the above and that I can call in each view. I have tried doing the following. I think I am on the right track but I don't know how to do the equivalent of "model => model.Note.Created" Can someone give me some advice on how I can do this and what I would need to replace the text inside the parenthesis with. I don't have a model but I can do this some other way so the hidden field will go look at my class to get the correct DisplayName just like it does above?

 namespace ST.WebUx.Helpers.Html
   {
    using System.Web.Mvc;
    using System.Web.Mvc.Html
    using System.Linq;

public static class StatusExtensions
{
    public static MvcHtmlString StatusBox(this HtmlHelper helper, RowInfo RowInfo )
    {
        return new MvcHtmlString( 
           "Some things here ... " +
           System.Web.Mvc.Html.InputExtensions.Hidden( for created field ) +
           System.Web.Mvc.Html.InputExtensions.Hidden( for modified field ) );
    }

}

解决方案

You could write a strongly typed helper taking a λ-expression:

public static class StatusExtensions
{
    public static IHtmlString StatusBox<TModel, TProperty>(
        this HtmlHelper<TModel> helper, 
        Expression<Func<TModel, TProperty>> ex
    )
    {
        return new HtmlString(
           "Some things here ... " +
           helper.HiddenFor(ex));
    }
}

and then:

@Html.StatusBox(model => model.RowInfo.Created)


UPDATE:

As requested in the comments section here's a revised version of the helper:

public static class StatusExtensions
{
    public static IHtmlString StatusBox<TModel>(
        this HtmlHelper<TModel> helper,
        Expression<Func<TModel, RowInfo>> ex
    )
    {
        var createdEx = 
            Expression.Lambda<Func<TModel, DateTime>>(
                Expression.Property(ex.Body, "Created"), 
                ex.Parameters
            );
        var modifiedEx =
            Expression.Lambda<Func<TModel, DateTime>>(
                Expression.Property(ex.Body, "Modified"),
                ex.Parameters
            );

        return new HtmlString(
            "Some things here ..." + 
            helper.HiddenFor(createdEx) + 
            helper.HiddenFor(modifiedEx)
        );
    }
}

and then:

@Html.StatusBox(model => model.RowInfo)

Needless to say that custom HTML helpers should be used to generate small portions of HTML. Complexity could grow quickly and in this case I would recommend you using an editor template for the RowInfo type.

这篇关于我怎样才能在一个扩展方法使用HTML辅助方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆