Quiero crear un botón que al hacer clic llame una acción. He usado Html.ActionLink
para crear un enlace que hace exactamente eso, pero no hay Html.ActionButton
. ¿Hay alguna otra manera?ActionButton en lugar de ActionLink
Respuesta
¿Hay alguna otra manera?
Se puede usar un html <form>
:
@using (Html.BeginForm("someAction", "someController"))
{
<button type="submit">OK</button>
}
Esto debería responder su pregunta.
Can I use an asp:Button like an Html.ActionLink?
utiliza CSS para hacer la mirada de enlace como un botón.
O use el botón dentro del formulario con los controles de formulario moviendo la página.
Utilice esta:
@using (Html.BeginForm("yourAction", "yourController"))
{
<input type="submit" value="Some text"/>
}
@using (Html.BeginForm("ActionName", "ControllerName")){
<input type="button" value="Submit" />
}
a[type="button"] {
background-color: #d3dce0;
border: 1px solid #787878;
cursor: pointer;
font-size: 1.2em;
font-weight: 600;
padding: 7px;
margin-right: 8px;
width: auto;
text-decoration: none;
}
@Html.ActionLink("ActionName", "ControllerName",null, new {type = "button" })
Aquí hay un ejemplo usando el botón CSS de Bootstrap y aplicándolo contra el enlace para que se vea exactamente igual que el botón.
@Html.ActionLink("Select", "Select", new { id = patient.Id }, new { @class = "btn btn-primary", @style = "color:white" })
Puede escribir un enlace al controlador y ver de esta manera/saltar toda la notación de afeitar (trabajado para mí recientemente en mi localhost!):
<a href="../Home(controller)/Index(view)"><button type="submit">OK</button></a>
Esto se rompe si alguna vez cambias la configuración de tu ruta. El enfoque @ Html.ActionLink es dinámico, y funcionará sin depender de una determinada configuración de ruta. – Alexander
Esto parece muy simple - Me estoy perdiendo ¿alguna cosa?
@Html.ActionLink("About", "About","Home", new { @class = "btn btn-success" })
Para señalar lo obvio, esto se basa en tener clases css btn/btn-success (suponiendo que esto sea Bootstrap o similar). ActionLink crea , pero la pregunta buscaba realmente
uso formAction
<input type="submit" value="Delete" id="delete" formaction="@Url.Action("Delete", new { id = Model.Id })" />
marcado veces el botón podría haber anidado (por ejemplo, para un icono)
Esto podría ser útil para algunas personas:
<button type="button" onclick="location.href='@Url.Action("Action", "Controller")'" class="btn btn-success">My button name <i class="glyphicon glyphicon-search"></i></button>
type = se requiere un botón para evitar que la página se envíe ... Otra posible solución (si piensa tipo = botón es extraño porque el elemento ya es un botón) sería llamar a event.preventDefault() en el controlador de OnClick javascript.
<button onclick="event.preventDefault(); location.href='@Url.Action("Action", "Controller")'" class="btn btn-success">My button name <i class="glyphicon glyphicon-search"></i></button>
Sé que esto es un viejo tema, pero esto me ayudó:
@Html.ActionLink("New member", "Create", null, new {@class = "btn btn-default"})
- 1. ActionLink htmlAttributes
- 2. ActionLink para mostrar los parámetros en URL en lugar de querystring?
- 3. MVC MapPageRoute y ActionLink
- 4. creando https ActionLink
- 5. ActionLink CS1026:) esperado
- 6. Raw ActionLink linkText
- 7. ActionLink con múltiples parámetros
- 8. Variable de Javascript en razor ActionLink
- 9. Razor actionlink autogenerating? Length = 7 en URL?
- 10. ActionLink fuertemente tipado en Asp.Net MVC 2?
- 11. MVC3 - Ajax actionlink - OnBegin, onComplete
- 12. ASP MVC Areas y ActionLink
- 13. Acción T4MVC fuertemente tipada/ActionLink
- 14. jquery datatables actionlink cómo agregar
- 15. MVC3 Cómo deshabilitar/habilitar ActionLink
- 16. Cómo anular el comportamiento de ActionLink
- 17. MVC3 Actionlink con diálogo de confirmación
- 18. ASP.NET MVC 3: ¿Cómo forzar a un ActionLink a hacer un HttpPost en lugar de un HttpGet?
- 19. Ayuda personalizada de ActionLink que sabe en qué página está
- 20. ASP.NET MVC ActionLink fuera del área
- 21. actionlink fuertemente tipado con asp.net mvc beta?
- 22. ASP.NET MVC ActionLink con LinkText = URL absoluta
- 23. System.Web.Mvc.HtmlHelper' no contiene una definición para 'ActionLink'
- 24. ASP.NET MVC agregar clase css a actionlink
- 25. MVC 4 ActionLink Dictionary htmlAttributes no funciona
- 26. ASP.NET MVC pasar un ID en una ActionLink al controlador
- 27. Eliminar ActionLink con el cuadro de diálogo de confirmación
- 28. ¿Es posible usar un ActionLink que contenga un elemento?
- 29. ASP MVC3 inserte la etiqueta html dentro de actionlink
- 30. Creación de un ActionLink con un DateTime en la cadena de consulta en ASP.NET MVC
(Sólo nitpicking) también añadir 'role = "botón"' para los atributos para ser el 100% de arranque reqs compatible – Alex