2012-03-23 36 views

Respuesta

18

Si conoce en el lado del servidor que el enlace no está disponible, entonces simplemente hacer que un mensaje de que la acción no está disponible:

@if(condition) 
{ 
    @Html.ActionLink("Delete", "Delete", new { id = @Model.Id}) 
} 
else 
{ 
    <text>Action is not available</text> 
} 

De lo contrario sólo se puede desactivar un enlace con

para que funcione entre navegadores: Should the HTML Anchor Tag Honor the Disabled Attribute?

13

Para desactivar una "una" etiqueta que puede hacer:

@Html.ActionLink("Delete", "Delete", new { id = @Model.Id}, new { onclick = "javascript:return false;" }) 

O puede usar jQuery:

@Html.ActionLink("Delete", "Delete", new { id = @Model.Id}, new { class = "linkdisabled" }) 

CSS:

.linkdisabled{ 
    cursor:text; 
} 

JQuery:

$function(){ 
    $(".linkdisabled").click(function(){ 
     return false; 
    } 
} 
+0

Hola, buena solución ... pero ¿hay alguna manera de hacer que el texto no se pueda hacer clic? Ahora aparece como un enlace pero no va a ninguna parte. Entonces, ¿hay alguna manera de cambiar su apariencia? Gracias – Scorpion

+0

Puede agregar un atributo de estilo o usar una clase css para hacer eso: –

+0

¡Gracias! Buena solución! –

3

Tal vez usted puede crear su propia interfaz de usuario de tipo MvcHtmlString

public static MvcHtmlString MyActionLink(this HtmlHelper helper, bool isClickable, string altText, RouteValueDictionary routeValues, object htmlAttributes = null) 
{ 
    // some logic with isClickale parameter here 
    if(isClickable == false) 
    {} 

    return new MvcHtmlString(helper.ToHtmlString()); 
} 

y utilizarlo en su Ver

@Html.MyActionLink(// some parameters here) 

Pero nunca lo he probado. Intente encontrar algo sobre MvcHtmlString en Google.