How can I get just href part of Html.ActionLink result text
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 ?
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.
精彩评论