1

ASP.NET MVC 4 を使用して Web アプリケーションを作成しています。リンクを作成するために使用する場合Html.ActionLink、パラメーターでアクション リンクに data-anything 属性を渡すことができhtmlAttributesます。しかしdata-、私は使用できませんdata_。代わりに使用する必要があります。にActionLink変わるdata_ようdata-です。カスタムヘルパーでこれを行うにはどうすればよいですか? htmlAttributes一般に、ヘルパーに渡されたものを変更するにはどうすればよいですか?

public static MvcHtmlString AuthorizeModalLink(this HtmlHelper Helper, string Text, object htmlAttributes)
{
    var builder = new TagBuilder("a");
    builder.MergeAttributes(new RouteValueDictionary(htmlAttributes));
    return MvcHtmlString.Create(builder.ToString(TagRenderMode.StartTag) + Text + builder.ToString(TagRenderMode.EndTag));
}

前もって感謝します。

4

1 に答える 1

3

IDictionary<string, object> htmlAttributesパラメータがある場合は、そのまま使用できます"data-foo"

data_foo匿名オブジェクトを使用するときに使用します。アンダースコアをハイフンに置き換える関数があります。HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes)

例:

public static HtmlString CustomCheckboxFor<TModel>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, bool>> expression, object htmlAttributes)
{
    return CustomCheckboxFor(htmlHelper, expression, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
}

public static HtmlString CustomCheckboxFor<TModel>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, bool>> expression, IDictionary<string, object> htmlAttributes)
{
    string controlHtml = htmlHelper.CheckBoxFor(expression, htmlAttributes).ToString();

    return htmlHelper.FormItem(expression, controlHtml);
}
于 2013-10-01T14:51:55.197 に答える