Would enter Glyphicons Boostrap instead of "Edit" in the code below. Could you give me an example to do.
#Html.ActionLink("Edit", "Edit", new { id = Model.id_rod }) |
To bring up the image instead of writing.
If using Bootstrap 3:
<a href="#Url.Action("Edit", new { id = Model.id_rod })">
<i class="glyphicon glyphicon-pencil"></i>
<span class="sr-only">Edit</span>
</a>
Note the use of sr-only will allow users of screen readers and search engines to know what the link is for.
I made helper for easier re-use
Helper Method Extension class
namespace Extensions {
public static class HtmlExtensions {
public static MvcHtmlString FALink(this HtmlHelper htmlHelper, string action, string controller, string link_text, string fa_class, string btn_css_classes = "", string button_id = "", object route_values = null) {
// declare the span for the glyphicon
TagBuilder span = new TagBuilder("span");
span.AddCssClass($"fa fa-{fa_class}");
span.MergeAttribute("aria-hidden", "true");
// declare the anchor tag
TagBuilder anchor = new TagBuilder("a");
// Add the href attribute to the <a> element
if (string.IsNullOrEmpty(controller) || string.IsNullOrEmpty(action))
anchor.MergeAttribute("href", "#");
else
anchor.MergeAttribute("href", new UrlHelper(HttpContext.Current.Request.RequestContext).Action(action, controller, route_values));
// Add the <span> element and the text to the <a> element
anchor.InnerHtml = $"{span} {link_text}";
anchor.AddCssClass(btn_css_classes);
anchor.GenerateId(button_id);
// Create the helper
return MvcHtmlString.Create(anchor.ToString(TagRenderMode.Normal));
}
}
}
Make sure you include the namespace in the View so method is available
Example usage in Razor View (Leave area empty string if your not using areas)
#Html.FALink("Index", "ControllerNameHere", "Back to List", "th-list", "btn btn-info", "btn_back_to_list", new {area=""})
While I use Font Awesome you could easily replace fa with glyphicon class to use the bootstrap glyphicons.
If no controller or Action is passed in, it uses # as the link location so it will play nice with drop down interaction
Related
i want to know that how to pass html attributes to #Html.ActionLink helper function
i want to do like that
<a class="accordion-toggle" data-toggle="collapse" data-parent="#leftMenu" href="/Admin/UserManagment">
<i class="icon-user"></i>User Managment
</a>
how to pass that italic html attribute
You could use #Url.Action() as an alternative.
In this case your code would look like this:
<a class="accordion-toggle" data-toggle="collapse" data-parent="#leftMenu"
href="#Url.Action("UserManagment", "Admin")">
<i class="icon-user"></i>User Managment
</a>
Edit:
Out of interest I did a bit of research and found this answer. Which explains it isn't possible to use the Html.ActionLink for this purpose:
The Html.ActionLink helper HTML encodes the link text which prevents
you from embedding HTML in the link text.
There is no way to wrap html code inside the anchor tag using #Html.ActionLink
You can use #Url.Action
<a href="#Url.Action("UserManagment", "Admin")" ...>
<i class="icon-user"></i>User Managment
</a>
Or create an extension method:
public static IHtmlString MyActionLink(this HtmlHelper helper,
string value,
Dictionary<string, string> attributes,
string innerHtml)
{
var aBuilder = new TagBuilder("a");
foreach (var attr in attributes)
{
aBuilder.MergeAttribute(attr.Key, attr.Value);
}
aBuilder.InnerHtml += innerHtml + value;
return new HtmlString(aBuilder.ToString(TagRenderMode.Normal));
}
And use it in your views like this
#{
var attributes = new Dictionary<string, string>
{
{ "data-toggle", "collapse" },
{ "data-parent", "#leftMenu" },
{ "href", Url.Action("UserManagement", "Admin") }
};
var innerHtml = "<i class='icon-user'></i>";
}
#Html.MyActionLink("User Managment", attributes, innerHtml)
<li class="rtsLI" id="Summary"><span class="rtsTxt">Test</span></li>
Above I am replacing with following actionlink:
<li class="rtsLI" >#Html.ActionLink("test1", "Index", new { Area = "Area1", Controller = "controller1" }, new { #class = "rtsLink rtsTxt"})</li> "
At first css is working fine. But when using Actionlink, css not working. Thanks
The standard ActionLink helper always HTML encodes the link text. This means that you cannot use it if you want to render HTML inside. You have 3 possibilities:
Modify your CSS so that you don't need a span inside the link and so that the rtsTxt class could directly be applied to the link
Write a custom ActionLink helper that doesn't HTML encode the text and which would allow you to generate the same markup:
public static class ActionLinkExtensions
{
public static IHtmlString ActionLinkUnencoded(
this HtmlHelper htmlHelper,
string linkText,
string actionName,
object routeValues,
object htmlAttributes
)
{
var urlHelper = new UrlHelper(htmlHelper.ViewContext.RequestContext);
var link = new TagBuilder("a");
link.MergeAttributes(new RouteValueDictionary(htmlAttributes));
link.Attributes["href"] = urlHelper.Action(actionName, routeValues);
link.InnerHtml = linkText;
return new HtmlString(link.ToString());
}
}
and then:
<li>
#Html.ActionLinkUnencoded(
"<span class=\"rtsTxt\">User Security</span>",
"index",
new { area = "Tools", controller = "UserSecurity" },
new { #class = "rtsLink" }
)
</li>
Use the Url.Action helper:
<li class="rtsLI">
<a href="#Url.Action("index", new { area = "Tools", controller = "UserSecurity" })" class="rtsLink">
<span class="rtsTxt">User Security</span>
</a>
</li>
Best option will be to use #Url.Action extension method
<li class="rtsLI" id="Summary"><span class="rtsTxt">User Security</span></li>
Write code this way:
<li class="rtsLI" >#Html.ActionLink("<span class='rtsTxt'>User Security</span>", "Index", new { Area = "Tools", Controller = "UserSecurity" }, new { #class = "rtsLink"})</li>`
I have this link in clean HTML. I want to make it into a MVC ActionLink. But how is that possible. I cant understund, seems there is no easy way. OR?
<li class="active"><i class="icon-home icon-large"></i> Dashboard</li>
Any ideas?
You have several options for doing this.
Option 1:
<li class="active"><i class="icon-home icon-large"></i> Dashboard</li>
Option 2:
Write your own extension method for HtmlHelper, that takes in a String/MvcHtmlString and put it into the link tag, without html encoding it.
public static MvcHtmlString SpecialActionLink(this HtmlHelper html, String action, String controller, String innerHtml) {
TagBuilder tag = new TagBuilder("a");
tag.MergeAttribute("href", new UrlHelper(html.ViewContext.RequestContext).Action(action, controller));
tag.InnerHtml = innerHtml;
return new MvcHtmlString(tag.ToString(TagRenderMode.Normal));
}
(Haven't tested it, but should give you an idea on how to do it.)
<li class="active">
#Html.ActionLink("Dashboard", //Title
"Index", // Action
"Home", // Controller
new { #class = "home-icon"} // htmlAttributes
)
</li>
.home-icon{ /*Some CSS to set the background image (not tested) */
padding:16px;
background:transparent url('path-to-image.png') .... ;
}
You Can't use ActionLink. only way is #url.action
<span>This is a test</span>
#Html.ActionLink("Edit Me", "Edit", new { id=item.ID })
In the above example, how do I make "Edit Me" bold? I tried placing <b></b> around the text but the bold tags are displayed literally. Instead of "Edit Me", the link displays "<b>Edit Me</b>".
Thanks!
PS - I'm using the MVC 4 beta but I don't see a tag that is that specific.
The easiest way is using the style attribute like:
#Html.ActionLink("Edit Me", "Edit", new { id=item.ID, style="font-weight:bold;" })
or you can set a class which you define in your css to be bold
#Html.ActionLink("Edit Me", "Edit", new { id=item.ID, #class="yourBoldClass" })
CSS: .yourBoldClass { font-weight: bold; }
I dont usually apply styles to an ACtionLink method because i think it is not a clean approach. Instead i will use a css class and define my style there and use that it in my link like this
#Html.ActionLink("Edit Me", "Edit","yourControllerName", new { id=item.ID },new {#class="yourClassName"})
and have a css class like this
.yourClassName
{
font-weight:bold;
}
you are using this constructor here
public static MvcHtmlString ActionLink(
this HtmlHelper htmlHelper,
string linkText,
string actionName,
string controllerName,
Object routeValues,
Object htmlAttributes
)
Here is the documentation : http://msdn.microsoft.com/en-us/library/dd504972.aspx
You can just put your url between <b></b> like that:
<b> #Html.ActionLink("Edit Me", "Edit", new { id=item.ID }) </b>
but the version with css is better
You cannot insert html in the link text with Html.ActionLink.
Write the <a> "by hand" (or create a new helper):
<b>"Edit Me"</b>
Or add a class to the link and do the "bolding" with CSS.
I'm new to MVC and would like to add a link to something like ~/Destinations/35, where it would refer to the Index view of the Destinations controller, and 35 is the ID of the destination to be displayed.
Neither ActionLink() or RouteLink() appear to allow me to create a link such as this.
Also, I tried something like this:
<table>
#foreach (var d in ViewBag.Results)
{
<tr>
<td>
#Html.ActionLink(
String.Format("<b>{0}</b>", #Html.Encode(d.Title)),
"Details", "Destinations")
</td>
</tr>
}
</table>
But I get the following error on the ActionLink line, which I don't understand.
'System.Web.Mvc.HtmlHelper' has no applicable method named 'ActionLink' but appears to have an extension method by that name. Extension methods cannot be dynamically dispatched. Consider casting the dynamic arguments or calling the extension method without the extension method syntax.
Can someone help me create this link?
The first problem with your code is that you are trying to use HTML in the link text (the <b> tags) which is not possible because by design it always HTML encodes.
So assuming you didn't want HTML in the link you could do this:
#Html.ActionLink(d.Title, "Details", "Destinations", new { id = "35" }, null)
And assuming you need HTML inside the anchor you have a couple of possibilities:
Write a custom ActionLink helper which won't HTML encode the text (recommended) and then use like this:
#Html.MyBoldedActionLink(d.Title, "Details", "Destinations", new { id = "35" }, null)
Something along the lines:
<a href="#Url.Action("Details", "Destinations", new { id = "35" })">
<b>#d.Title</b>
</a>
and since I recommend the first approach here's a sample implementation of the custom helper:
public static class HtmlExtensions
{
public static IHtmlString MyBoldedActionLink(
this HtmlHelper htmlHelper,
string linkText,
string actionName,
string controllerName,
object routeValues,
object htmlAttributes
)
{
var anchor = new TagBuilder("a");
anchor.InnerHtml = string.Format("<b>{0}</b>", htmlHelper.Encode(linkText));
var urlHelper = new UrlHelper(htmlHelper.ViewContext.RequestContext);
anchor.Attributes["href"] = urlHelper.Action(actionName, controllerName, routeValues);
anchor.MergeAttributes(new RouteValueDictionary(htmlAttributes));
return new HtmlString(anchor.ToString());
}
}