10

Is there a way to create a strongly typed T4MVC ActionLink with a hash/pound/fragment in it?

For example, here is the link I'd like to create:

<a href="/Home/Index#food">Feed me</a>

But there's no extension to the T4MVC object that can do this.

<%= Html.ActionLink("Feed me", T4MVC.Home.Index()) %>

So, what I end up having to do is create an action, and then embed it that way:

<a href="<%= Url.Action(T4MVC.Home.Index()) %>"#food>Feed me</a>

This isn't very desirable. Anyone have any ideas/suggestions?

Thanks in advance

Dan Atkinson
  • 11,391
  • 14
  • 81
  • 114
  • 2
    You would have to edit the T4 template to add this. That shouldn't be to hard. But in my opinion you should use the url.Action helper (as your example). I feel it is a lot more readable. Personally I don't like to generate such simple html as a tag in a helper. – Mattias Jakobsson May 27 '10 at 12:26
  • Any reason why you don't like to generate it? – Dan Atkinson May 27 '10 at 12:28
  • 1
    because I feel that generating html is the responsibility of the view itself. You don't need a helper to write such simple thing as a tag. I just think its a lot more readable if you write the tag yourself. – Mattias Jakobsson May 27 '10 at 12:36
  • You could argue the same about RenderPartial, SelectList and just about any other Html helper there is, but unless you've got a really simple site, then you're going to need something behind the scenes. – Dan Atkinson May 27 '10 at 12:43
  • No. RenderPartial just use another view, no problems there. You can say the same about selectlist, but there is a good reason to use it, you can strongly type your id's so they will be bound in the right way. In that case the pros overweights the cons in my opinion. But there is no reason to use a helper to render a simple tag. I never use html helpers for that kind of stuff. This is just my personal opinion and I can understand that some may disagree. But in my opinion your last example (with url helper) is a lot more elegant then the first one, simply more readable. – Mattias Jakobsson May 27 '10 at 12:55

4 Answers4

9

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) %>
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • 4
    This was already available in MVC v1, but not with the T4MVC additions. My main reason for having T4MVC was to get away from the magic strings that are present in your example. – Dan Atkinson May 27 '10 at 15:06
8

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.

Arnis Lapsa
  • 45,880
  • 29
  • 115
  • 195
  • 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. – Dan Atkinson May 27 '10 at 15:09
  • 1
    See below, David Ebbo's answer, this is now in T4MVC – Paul Tyng May 28 '13 at 00:26
2

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 :)

Paul Tyng
  • 7,924
  • 1
  • 33
  • 57
David Ebbo
  • 42,443
  • 8
  • 103
  • 117
  • Hi David. Thanks for the answer. Yes, I think that freezing it for .NET 3.5 will definitely help in encouraging people to move to .NET 4/MVC 2. Certainly, it'll make it easier with named params. – Dan Atkinson Jun 01 '10 at 08:33
1

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

Community
  • 1
  • 1
Rebecca
  • 13,914
  • 10
  • 95
  • 136