ActionLink htmlAttributes
-
29-09-2019 - |
Pregunta
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>
no funciona - ¿Por qué
@Html.ActionLink("Edit", "edit", "markets", new { id = 1 }, new {@class="ui-btn-right", data-icon="gear"})
Parece que no se puede pasar algo así como datos-icon = "engranajes" en htmlAttributes?
Sugerencias?
Solución
El problema es que su data-icon
propiedad de objeto anónimo tiene un nombre no válido. C # propiedades no pueden tener guiones en sus nombres. Hay dos maneras que usted puede conseguir alrededor de eso:
Usar un guión en lugar del guión (MVC reemplazará automáticamente el subrayado con un guión en el HTML emitida):
@Html.ActionLink("Edit", "edit", "markets",
new { id = 1 },
new {@class="ui-btn-right", data_icon="gear"})
Utilice la sobrecarga que lleva en un diccionario:
@Html.ActionLink("Edit", "edit", "markets",
new { id = 1 },
new Dictionary<string, object> { { "class", "ui-btn-right" }, { "data-icon", "gear" } });
Otros consejos
Reemplazar el guión deseado con un subrayado; automáticamente se representa como un guión:
@Html.ActionLink("Edit", "edit", "markets",
new { id = 1 },
new {@class="ui-btn-right", data_icon="gear"})
se convierte en:
<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"})
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow