I like to do something like the following in my MVC View:
#Html.ActionLink("How do <i>This</i>", "Vtd", "Crs", new { Text= Html.Encode("How it works"}, null)
Notice how I want This to be in italics as a hyperlink
As far as Html.ActionLink() just render tag maby that's not the best way to achive your goal.
I belive in your case it's better to use Url.Action() helper like this:
<a href="#Url.Action("Vtd", "Crs", new { Text= Html.Encode("How it works"}, null })">
How do <i>This</i>
</a>
Related
I am using MVC3, ASP.NET4.5 and Razor.
I have some Html.Action Code, and would ideally like to convert it to Html.ActionLink as I am thinking of using a security extension function that has been written for ActionLinks.
My code is:
<span class="fa fa-pencil ss-prime ss-cmd" title="Edit Order"></span>
Can this be implemented using an ActionLink?
The "fa fa-pencil" code is a webfont, and I need this.
Thanks in advance
If you use an action link, you may need to modify your styles slightly:
Html.ActionLink("edit order", "Edit", "Order"
, new { id= 1 }
, new { #class="fa fa-pencil" }) // <-- html attributes
This will create an a href, but attach the css styles to the a tag, not the span.
<a class="fa fa-pencil" href="/Order/Edit/1">edit order</a>
Html.ActionLink("Edit", "ActionResult", new { CustomerId= DataBinder.Eval(c.DataItem, "CustomerId") }, new { target = "_blank" })
How can i make html.actionlink as image (set image)
Any help will be greatly apreciated.
An ActionLink is, pretty much by definition, a link. The Html.ActionLink method you're calling even accepts the text of the link as its first parameter, so you can expect it to be a text link.
But with slightly more manual HTML, you can create an a tag around an img tag using the same target URL. Just use Url.Action instead of Html.ActionLink. Maybe something like this:
<a href="#Url.Action("ActionName", "ControllerName", new { CustomerId= DataBinder.Eval(c.DataItem, "CustomerId") })" target="_blank">
<img src="yourImage.png" alt="Image Text" />
</a>
I have a RouteLink constructed like so
<p class="articleLink">
#MvcHelper.Html.RouteLink(article.Title, "Article_Route", new RouteValueDictionary() { { "articleId", article.Id }, { "seoUrl", article.SeoUrl } }))
</p>
However, article.Title could potentially contain HTML i.e. the value could be <em>Sample</em> Title which in turn gets rendered like so
<em>Sample</em> Title
Is there any way to prevent the HTML from being escaped, and instead to be treated as actual HTML? Or do I need to create a standard HTML <a href... link in this case (thus losing all the niceties associated with the RouteLink helper).
If you want HTML inside your anchor don't use the Html.RouteLink (because it will HTML encode the link text by default as you noticed) instead of build your a tag by hand with using Url.RouteUrl to generate the url:
<p class="articleLink">
<a href="#(Url.RouteUrl("Article_Route",
new RouteValueDictionary()
{ { "articleId", article.Id },
{ "seoUrl", article.SeoUrl } }))">
#Html.Raw(article.Title)
</a>
</p>
Or you can create your own non encoding RouteLink helper.
You can use HtmlHelper.Raw(). Having said that, it is probably not a good idea to keep any HTML in your model/view model, which is against the principle of separation of model and presentation, also a security hazard.
Could someone show me the syntax of an Html.ActionLink that will produce a hyperlink that looks like this:
<a h ref="/mycontroller/myaction/67">mylinktext</a>
thanks.
Terrence
It depends more on how you set up your routes. But if you only have the default route and myaction takes one parameter named id it can look like this:
<%=Html.ActionLink("mylinktext", "myaction", "mycontroller", new { id = 67 }, null) %>
Or, if you want and have mvc features or mvc2, it can look like this:
<%=Html.ActionLink<mycontroller>(x => x.myaction(67), "mylinktext")%>
To generate a link, use the HtmlHelper and the Action extension...
<%= Html.ActionLink ("mycontroller", "myaction", new { id = 67 }) %>
I'd like this output:
<a href="\Catalog\Flooring">
<img src="http://site.com/dot.jpg" width="100px" height="100px" alt="" />
<span>Some text here</span>
</a>
using a RouteLink similar to:
<%= Html.RouteLink(myFPV.ProductTypeName, "CatalogType", new { controller = "Catalog", action = "Types", group = myFPV.ProductGroupName, type = myFPV.ProductTypeName })%>
I cannot figure out how to add an <img> and <span> (with text) tags inside my <a> tag.
Make sense?
The first parameter of the RouteLink method is for the link text. But unfortunately, it gets encoded automatically, so you cannot send <, > characters to it. (Well, you can. But they'd get encoded.)
Take a look at this page.
Route values are URL encoded automatically. For example, the string “Hello World!” is
encoded to “Hello%20World!”.
Rendering Image Links
Unfortunately, you can’t use the
Html.ActionLink() helper to render an
image link. Because the
Html.ActionLink() helper HTML encodes
its link text automatically, you
cannot pass an tag to this
method and expect the tag to render as
an image.
Instead, you need to use the
Url.Action() helper to generate the
proper link. Here’s how you can
generate a delete link with an image:
<a href="<%= Url.Action("Delete") %>">
<img src="../../Content/Delete.png" alt="Delete" style="border:0px" />
</a>
I suggest use the Url.RouteUrl.
<a href="#Url.RouteUrl("Default", new { action = "Index", controller = "Department", id = 1 })">
<img src="http://site.com/dot.jpg" width="100px" height="100px" alt="" /> </a>
There is a better option to do that. you should try some thing like
<a href="#Url.RouteUrl("index-lang")">
<img src="~/images/logo.png" alt="">
</a>
where the "index-lang" is the route name in route mapping table.