In ASP.Net MVC how do you generate facebook links? - asp.net-mvc

In ASP.Net MVC, how do you generate the following link?
<a class="facebook" rel="nofollow" target="_blank" href="http://www.facebook.com/sharer.php?u=HTTP://myreallycoolsite.com/somegroup/somechildgroup/some_title/">some_title</a>
The current page is
HTTP://myreallycoolsite.com/somegroup/somechildgroup/some_title/
and it needs to be used as a parameter of the external link.

Here it is in razor:
#{
// you can inline this instead
var Param = Url.Encode(Url.Action("action", "controller", new{ /*params*/ });
^^^^^
// or for the current page's URL (hat tip #Dismissile)
Param = Url.Encode(Request.Url.ToString());
}
<a class="facebook"
rel="nofollow"
target="_blank"
href="http://www.facebook.com/sharer.php?u=#Param">some_title</a>
^^^^^^
If you're not using Razor, it looks like this (or at least close to this):
<%
// you can inline this instead
var Param = Url.Encode(Url.Action("action", "controller", new{ /*params*/ });
^^^^^
// or for the current page's URL (hat tip #Dismissile)
Param = Url.Encode(Request.Url.ToString());
%>
<a class="facebook"
rel="nofollow"
target="_blank"
href="http://www.facebook.com/sharer.php?u=<%=Param%>">some_title</a>
^^^^^^

Create the link as normal and put #Request.Url.ToString() in it. If you need to dynamically put the some_title text in there then you will need to do one of a few different things. If it is part of the route, then you could pull it from RouteData. If it is the title of your page you could probably use ViewBag.Title. If it is completely arbitrary you might just need to use a regex.
<a class="facebook" rel="nofollow" target="_blank" href="http://www.facebook.com/sharer.php?u=#Request.Url.ToString()">some_title</a>

Try this one
<a href="#"
onclick="
window.open(
'https://www.facebook.com/sharer/sharer.php?u='+encodeURIComponent(location.href),
'facebook-share-dialog',
'width=626,height=436');
return false;">
Share on Facebook
</a>

Related

create relative links with Thymeleaf

I have the following code which loops through a list of countries and creates a href links
<div class="container" th:each="country: ${countryList}">
<a th:href="${country.countryAbbr}"><div clss="row" th:text="${country.country}"></div></a>
</div>
The current page url is "localhost:8080/directory", and the generated url is showing as
"localhost:8080/us"
How can I make the url show as "localhost:8080/directory/us"
I want "us" to be added to the current url of the page.
Try create the following code in your Controller class.
#RequestMapping(value="/", method=RequestMethod.GET)
public String rootGet() {
return "redirect:/directory";
}
You can use ServletUriComponentsBuilder:
<div th:with="urlBuilder=${T(org.springframework.web.servlet.support.ServletUriComponentsBuilder)}"
class="container" th:each="country: ${countryList}">
<a th:href="${urlBuilder.fromCurrentRequest().path(${country.countryAbbr}).toUriString()}">
<div clss="row" th:text="${country.country}"></div>
</a>
</div>
Give a try to this one
<div class="container" th:each="country: ${countryList}">
<a th:href="#{${country.countryAbbr}}"><div clss="row" th:text="${country.country}"></div></a>
</div>
Using # usually resolves the default context but I am not aware of your environment.
For example, if you had Tomcat .war file, and your application would be hosted at localhost:8080/myApp, you would want result /myApp/us rather then /us. # would do that trick.
If above is not relevant for you, use this one:
<div class="container" th:each="country: ${countryList}">
<a th:href="#{'/directory/' + ${country.countryAbbr}}"><div clss="row" th:text="${country.country}"></div></a>
</div>

Trying to convert this Html.Action Code to Html.ActionLink

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>

Set image to Html ActionLink

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>

on html.actionlink click go to previous page

Currently in a link
Customer/businessunit/RepresentativeDetails?RepresentativeId=cd3a7263-78f7-41bd-9eb0-12b30bc1059a
I have following code for view
#Html.ActionLink("Back to List", "Index")
which takes me to this link
customer/businessunit/index
but rather that going to index page I want to go to previous page when the actionlink is clicked, which is
Customer/businessunit/BusinessUnitDetails/c4a86253-a287-441e-b83d-71fbb6a588bc
How do I create an actionlink that directs me to previous page?
something like #Html.ActionLink("Back to Details", //go to previous page)
Unless you're tracking what the previous page is on the server, why not just use the browser's internal history? In that case there wouldn't be a need for server-side code. You could just use something like this:
Back to Details
Or, separating the code from the markup:
Back to Details
<script type="text/javascript">
$(document).on('click', '#backLink', function () {
history.go(-1);
});
</script>
This would send the user back to whatever was the last page in their browser history. (Of course, if they reached that page from any other source then it wouldn't take them "back to details" but instead just "back".)
If you still want to use ActionLink you can do something like as suggested by JuanPieterse
#Html.ActionLink("Back to previous page", null, null, null, new { href = Request.UrlReferrer})
You can use action in controller too. See answers to similar question here
Don't use ActionLink for this... just do:
Back to List
...which will take the user back to wherever they were prior to the current page
If you don't like to use ActionLink or JavaScript, the href="#Request.UrlReferrer" will do the trick:
<div>
<a href="#Request.UrlReferrer" class="btn btn-default btn-lg" title="Back to list">
<span class="glyphicon glyphicon-arrow-left" aria-hidden="true"></span>
</a>
</div>
This is quite a bit after the fact, but I thought I'd contribute. Personally, I would tag my markup elements with a CSS class so I could just reuse the tag and be done with it.
Markup:
Back
Script:
<script type="text/javascript">
$(function () {
$('.go_back').click(function (e) {
e.preventDefault();
history.go(-1);
});
});
</script>
Use ActionLink:
#Html.ActionLink("بازگشت", null, null, null, new { href = Request.UrlReferrer })
i know this is too late but i got this answer.
<button type='reset' class='btn btn-danger' onclick='window.location.go(-1);'><i class='fa fa-ban'></i> Cancel </button></div>

MVC - RouteLink and Image

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.

Resources