tags:

views:

80

answers:

2

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 ?

+4  A: 

Try this

<%=Url.Action("Action","Controller")%>
Malcolm Frexner
Thank you very much
Selçuk Yavuz
A: 

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.

TreeUK
But now you have a controller Name in the view ... not in the spirit of MVC and separation of concerns.
Martin
Agreed, it could be passed in via the view model, but given his sample, this is an improvement. One step at a time :p
TreeUK