【问题标题】:passing html to a function asp.net mvc将 html 传递给函数 asp.net mvc
【发布时间】:2011-10-28 16:40:02
【问题描述】:

我有 Html 辅助方法,创建一个复选框以及一些文本,我通过了。

@Html.CheckBoxFor(x => x.SomeProperty,@<text> <ul> <li> </li> </ul>           </text>}))


public static MvcHtmlString CheckBoxFor<TModel, TProperty>(this HtmlHelper<TModel> helper, Expression<Func<TModel, bool>> expression, ?? )
    {
        var chkBox = helper.CheckBoxFor(expression).ToString();

        return MvcHtmlString.Create(string.Format("<li>{0}</li><div>{1}</div>", chkBox, ??);
    }

那么我的方法的签名是什么。一些 lambda/表达式之类的。

我们将不胜感激。

问候

帕米德

【问题讨论】:

  • 将其定义为一个对象,运行它,然后查看运行时为您提供的类型。

标签: asp.net-mvc c#-4.0 html-helper


【解决方案1】:

我建议您查看templated razor delegates。因此,在您的情况下,助手可能看起来类似于:

public static MvcHtmlString CheckBoxFor<TModel>(
    this HtmlHelper<TModel> helper, 
    Expression<Func<TModel, bool>> expression,
    Func<object, HelperResult> template)
{
    var chkBox = helper.CheckBoxFor(expression).ToHtmlString();
    return MvcHtmlString.Create(
        string.Format("<li>{0}</li><div>{1}</div>", chkBox, template(null))
    );
}

在视图中:

@model MyViewModel
@using (Html.BeginForm())
{
    @Html.CheckBoxFor(
        x => x.SomeProperty,
        @<text><ul><li></li></ul></text>
    )
    <input type="submit" value="OK" />
}

【讨论】:

    【解决方案2】:

    只需string。它会进入string.Format,所以这会给你一个提示。

    【讨论】:

      猜你喜欢
      • 2020-01-09
      • 2011-08-24
      • 1970-01-01
      • 1970-01-01
      • 2010-09-14
      • 2011-02-04
      • 2018-02-18
      • 2023-04-05
      • 1970-01-01
      相关资源
      最近更新 更多