ActionLink htmlAttributes

≡放荡痞女 提交于 2019-11-26 12:56:08

问题


WORKS

<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\"})

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

Suggestions?


回答1:


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:

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" } });



回答2:


Replace the desired hyphen with an underscore; it will automatically be rendered as a hyphen:

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

becomes:

<form action="markets/Edit/1" class="ui-btn-right" data-icon="gear" .../>



回答3:


@Html.ActionLink("display name", "action", "Contorller"
    new { id = 1 },Html Attribute=new {Attribute1="value"})


来源:https://stackoverflow.com/questions/4108943/actionlink-htmlattributes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!