ActionLink htmlattributes
-
29-09-2019 - |
質問
作品
<a href="@Url.Action("edit", "markets", new { id = 1 })"
data-rel="dialog" data-transition="pop" data-icon="gear" class="ui-btn-right">Edit</a>
うまくいかない - なぜ?
@Html.ActionLink("Edit", "edit", "markets", new { id = 1 }, new {@class="ui-btn-right", data-icon="gear"})
data-icon = "gear"のようなものをhtmlattributesに渡すことはできないようですか?
提案?
解決
問題は、匿名のオブジェクトプロパティです data-icon
無効な名前があります。 C#プロパティは、名前にダッシュを持つことはできません。それを回避できる2つの方法があります:
Dashの代わりにアンダースコアを使用します(MVCは、エミストされたHTMLのダッシュにアンダースコアを自動的に置き換えます):
@Html.ActionLink("Edit", "edit", "markets",
new { id = 1 },
new {@class="ui-btn-right", data_icon="gear"})
辞書を取り入れるオーバーロードを使用します。
@Html.ActionLink("Edit", "edit", "markets",
new { id = 1 },
new Dictionary<string, object> { { "class", "ui-btn-right" }, { "data-icon", "gear" } });
他のヒント
目的のハイフンをアンダースコアに置き換えます。それは自動的にハイフンとしてレンダリングされます:
@Html.ActionLink("Edit", "edit", "markets",
new { id = 1 },
new {@class="ui-btn-right", data_icon="gear"})
なる:
<form action="markets/Edit/1" class="ui-btn-right" data-icon="gear" .../>
@Html.ActionLink("display name", "action", "Contorller"
new { id = 1 },Html Attribute=new {Attribute1="value"})
所属していません StackOverflow