As you know,
<%=Html.ActionLink("Back to List", "Index") %>
generates html like this : <a href="/Content/Index">Back To List</a>
But I need just href part.
I will use it in JS code and I do not want to write manually.
Can I gerenate what I need part ?
There is a difference. Html. ActionLink generates an <a href=".."></a> tag whereas Url. Action returns only an url.
ActionLink(HtmlHelper, String, String, String, String, String, String, Object, Object) Returns an anchor element (a element) for the specified link text, action, controller, protocol, host name, URL fragment, route values, and HTML attributes.
ActionLink is rendered as an HTML Anchor Tag (HyperLink) and hence it produces a GET request to the Controller's Action method which cannot be used to submit (post) Form in ASP.Net MVC 5 Razor. Hence in order to submit (post) Form using @Html. ActionLink, a jQuery Click event handler is assigned and when the @Html.
Try this
<%=Url.Action("Action","Controller")%>
Mathias's answer is what I use. ASP.NET MVC 2 gives you strongly types Url.Action too.
I find this most useful in javascript so:
<script type="text/javascript">
var urlToPostTo = '<%= Url.Action<HomeController>(h => h.ContactUs()) %>';
var someData = 'Some valuable data!';
$.post(urlToPostTo, someData, function()
{
alert('Successfully posted some data to some url');
});
</script>
This allows you to avoid putting hardcoded paths in your markup, leaving you with a slightly more maintainable solution.
That said, I'm still hoping that these will be compile time checked as normal when MVC 2 is finally released.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With