Create a T4MVC ActionLink with url fragment

放肆的年华 提交于 2019-11-29 13:22:10

This kind of approach is the only one i can think of that feels (to me) slightly better than writing anchor manually:

${Html.ActionLink("Feed me", T4MVC.Home.Index(), Fragment: "food")}

Apart from spark viewengine - it costs 1 good old htmlhelper extension method & named parameters.


I assume that this isn't available in the default viewengine? I've decided to write a quick extension for the ActionLink, but it's not elegant, and I would have liked any solution to be available to others in future versions of T4MVC.

Spark replaces <%=%> with ${}. Mentioned just because I prefer it (You should try it if You emphasize code elegance). C# 4.0 is required in order to use named parameters.

That's because I would like to avoid losing information to which parameter "food" argument maps.


And yeah, i strongly agree with Mattias Jakobsson.

In ASP.NET MVC 2.0 new helpers have been added that allow you to specify the fragment. Example:

<%= Html.ActionLink("Feed me", "Action", "Controller", null, null, "food", null, null) %>
David Ebbo

Update: This overload is included with T4MVC 2.6.56

Yes, for completeness we should probably add this to T4MVC. It should be easy to do, except we'll end up with a lot of overload if we start adding protocol/hostname as well.

Things would be easier if we stopped supporting Fx 3.5, because we could rely on default/named params, which help a lot with reducing overload hell. But I've been avoiding that step so far because not everyone is on 4.0 yet.

Maybe I should freeze the current version as the last Fx 3.5/MVC 1.x compatible, and then only support Fx 4.0/MVC 2 in newer builds (while keeping the old one up indefinitely). Anyway, I'm digressing :)

Rebecca

David Ebbo added this feature to T4MVC based on a similar question I made on StackOverflow.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!