ASP.NET MVC: generating action link with custom html in it - asp.net-mvc

How can I generate action link with custom html inside.
Like following:
<a href="http://blah-blah/.....">
<span class="icon"/>
New customer
</a>

You can use the UrlHelper class :
<a href="<% =Url.Action("Create","Customers") %>">
<span class="icon"/> New customer
</a>
The MSDN link is here : http://msdn.microsoft.com/en-us/library/system.web.mvc.urlhelper.aspx

For newer versions of .net,
replace
href="<% =Url.Action("Create","Customers") %>"
with
href="#Url.Action("Create","Customers")"
to give something like
<a href="#Url.Action("Create","Customers")">
<span class="icon"/> New customer
</a>

Related

ASP.NET MVC - Applying Html.ActionLink to an existing href

Basically, I'm just trying to change the href attribute to be the result of the ActionLink:
#Html.ActionLink("Home", "Index", "Dashboard", new { }, new { #class = "nav-link active" })
<a class="nav-link active" href="index.html">
<div class="sb-nav-link-icon"><i class="fas fa-home"></i></div>
Home
</a>
If I replaced the index.html with #Html.ActionLink("Home", "Index", "Dashboard"), then what happens is the HTML is not formed correctly.
Any suggestions would be appreciated.
Thanks!
#Url.Action("Index", "Dashboard"), sorry this can be closed. Of course as soon as I ask I find the answer.
I guess that this code will work as you want it to. You don't have to use #Html.ActionLink
<a class="nav-link active" href="Dashboard/Index">
<div class="sb-nav-link-icon"><i class="fas fa-home"></i></div>
Home
</a>

Using an HTML Action Link to target a div

I am new to ASP/MVC and I am having trouble figuring out how to link a div to a page in HTML markup. This is the current link in pure HTML. I want to accomplish this, but in razor syntax
<div class="col-md-2">
<a href="ambulance.html">
<div class="amb item">
<div class="tiletext">AMB</div>
<div class="tilesubtext">Ambulance</div>
</div>
</a>
</div>
I've been looking into action links, but if there is a better way to accomplish this, I'm open to it!
Possible duplicate. I'll add a bit of explanation that pertains to the question since it has to do with Razor:
What your backend developer needs is Url.Action helper. This will let you route the link through the MVC framework.
So say:
<div class="col-md-2">
<a href="#Url.Action("Cars", "Ambulance")">
<div class="amb item">
<div class="tiletext">AMB</div>
<div class="tilesubtext">Ambulance</div>
</div>
</a>
</div>
ASP.NET MVC: generating action link with custom html in it
Html.ActionLink method only creates anchor tags for some action method, you need Url.Action method. Using the rest of the markup is fine.
<div class="col-md-2">
<a href="#Url.Action("Index","Home")">
<div class="amb item">
<div class="tiletext">AMB</div>
<div class="tilesubtext">Ambulance</div>
</div>
</a>
</div>

ASP.NET MVC how to insert other html element inside #HTML.ActionLink

Normally, i would write something like this:
<li>#Html.ActionLink("Dashboard", "Index", "Account")</li>
To generate this:
Dashboard
What i want to know is whether it is possible to generate the following HTML
<i class="fa fa-dashboard"></i> Dashboard
Without having to create my own custom TagBuilder class.
Thanks for the help.
You can use UrlHelper.Action to generate the url:
<a href="#Url.Action("Index", "Account")">
<i class="fa fa-dashboard"></i> Dashboard
</a>

Adding custom html into Html.MenuItem link

I use Html.MenuItem helper in asp .net mvc. I want to use html inside link instead of text only. The helper below:
#Html.MenuItem("Announcement", "Index", "Announcement")
generates in html =>
<li> Announcement </li>
but I want to generate an html like =>
<li>
<a href="/Announcement">
<i class="icon-announcement"></i>
<span>Announcement</span>
</a>
</li>
How can I do that?
You can't do that immediately, because the HTML gets stripped if you pass it in as the parameter.
I suggest you do something like the following:
<li>
<a href='#Url.Action("Announcement", "Index")'>
<i class="icon-announcement"></i>
<span>Announcement</span>
</a>
</li>

Links not opening with left click

I am having a bit of a problem with my site, i am trying to add a simple link to an external site, but i can't get the link to open without right clicking and selecting open link.
This is the piece of code
<div class="brick1 odd">
<a href="https://www.google.co.uk/" class="nav-item"</a>
<div class="nav-hover"></div>
<i class="li_shop"></i>
<span>Shop now</span>
</a>
</div>
I know it's most likely something simple but i can't seem to pin it down
Thanks
Aha! The problem is because you are trying to link from an http to an https!
Your anchor tag needs to override the click handler:
<a href="https://www.google.co.uk/" class="nav-item" onclick="return !window.open(this.href,'WINDOW_NAME');" >
Should fix it!
But I would still fix the nesting.
try properly nesting everything, like so:
<div class="brick1 odd">
<a href="https://www.google.co.uk/" class="nav-item" onclick="return !window.open(this.href,'WINDOW_NAME');">
<div class="nav-hover">
<i class="li_shop">
<span>Shop now</span>
</i>
</div>
</a>
</div>
Syntax error
<a href="https://www.google.co.uk/" class="nav-item">
<div class="nav-hover"></div>
<i class="li_shop"></i>
<span>Shop now</span>
</a>

Resources