ActionLink的htmlAttributes [英] ActionLink htmlAttributes

查看:166
本文介绍了ActionLink的htmlAttributes的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<a href="@Url.Action("edit", "markets", new { id = 1 })" 
            data-rel="dialog" data-transition="pop" data-icon="gear" class="ui-btn-right">Edit</a>

并不奏效 - 为什么?

DOES NOT WORK - WHY?

@Html.ActionLink("Edit", "edit", "markets", new { id = 1 }, new {@class="ui-btn-right", data-icon="gear"})

看来你无法通过类似的数据图标=齿轮到htmlAttributes?

It seems you can't pass something like data-icon="gear" into htmlAttributes?

建议?

推荐答案

的问题是,您的匿名对象属性数据图标具有无效的名称。 C#属性不能有自己的名字破折号。有两种方法可以解决的:

The problem is that your anonymous object property data-icon has an invalid name. C# properties cannot have dashes in their names. There are two ways you can get around that:

使用下划线代替破折号(MVC将自动替换在发射HTML破折号下划线):

Use an underscore instead of dash (MVC will automatically replace the underscore with a dash in the emitted HTML):

@Html.ActionLink("Edit", "edit", "markets",
      new { id = 1 },
      new {@class="ui-btn-right", data_icon="gear"})

使用了发生在一个字典中的过载:

Use the overload that takes in a dictionary:

@Html.ActionLink("Edit", "edit", "markets",
      new { id = 1 },
      new Dictionary<string, object> { { "class", "ui-btn-right" }, { "data-icon", "gear" } });

这篇关于ActionLink的htmlAttributes的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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