Sign In| Join
Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Jan 05, 2010 10:18 PM by WhatThe12
Member
21 Points
62 Posts
Apr 10, 2008 01:07 PM|LINK
Hello,
Is there an option to add CSS Classes to Html Helper Methods like: Html.ActionLink. ???
To get someting like: <a href="go.aspx" class="active" >item</a>
368 Points
76 Posts
Apr 10, 2008 01:22 PM|LINK
Try this one:
Html.ActionLink<ControllerName>( c => c.SomeAction(), "item", new { class = "active" })
Apr 10, 2008 01:36 PM|LINK
Already gave that a try, but CLASS in the method couldn't be used!
Contributor
4392 Points
883 Posts
ASPInsiders
MVP
Apr 10, 2008 01:38 PM|LINK
use new { @class = "classname" }
or new { _class = "classname" }
5 Points
10 Posts
I've been looking for the answer to this too. In the mean time I'm using IDs, but it's not the best solution.
Html.ActionLink<ControllerName>( c => c.SomeAction(), "item", new { class = "active" }) doesn't work (I guess) because class is a reserved word.
Apr 10, 2008 01:39 PM|LINK
tgmdbm use new { @class = "classname" } or new { _class = "classname" }
Ah, if only i'd waited a few more seconds...
Thanks a lot!
Apr 10, 2008 02:01 PM|LINK
Tnx.
This works fine but i want to use the Actionlink in an other way.
like: Html.ActionLink("LinkName", "Action", "Controller", new { @class = "active" });
The link link hasnt got a CSS class now but the Class is put as attribute behind the Href url. ???
What do ik do wrong in the syntax ????
Apr 10, 2008 02:14 PM|LINK
eh?
That should output html just like what you wanted in the OP.
Please post the html it generates and the html you want to generate.
Thanks
6716 Points
1358 Posts
Apr 10, 2008 04:41 PM|LINK
I used the lambda syntax and it worked fine:
Html
using the string-only syntax, it does indeed emit the url with a querystring variable class=active. Smells like a bug to me.
Side by side source:
Paul
Apr 10, 2008 05:20 PM|LINK
i only ever use the lambda syntax so i never spotted it before.
Yeah, in the string version the object you pass in is the Route Data values. So adding class=active to the querystring is not a bug.
But not having an overload which takes htmlAttributes i think IS a bug.
Well spotted.
Don't forget to mark the posts which helped as answers.
toinedeboer
Member
21 Points
62 Posts
Html.ActionLink and CSS classes
Apr 10, 2008 01:07 PM|LINK
Hello,
Is there an option to add CSS Classes to Html Helper Methods like: Html.ActionLink. ???
To get someting like: <a href="go.aspx" class="active" >item</a>
maartenba
Member
368 Points
76 Posts
Re: Html.ActionLink and CSS classes
Apr 10, 2008 01:22 PM|LINK
Try this one:
Html.ActionLink<ControllerName>( c => c.SomeAction(), "item", new { class = "active" })
Order my book ASP.NET MVC 1.0 Quickly via http://www.packtpub.com/asp-net-model-view-controller-1-0-quickly/book
toinedeboer
Member
21 Points
62 Posts
Re: Html.ActionLink and CSS classes
Apr 10, 2008 01:36 PM|LINK
Already gave that a try, but CLASS in the method couldn't be used!
tgmdbm
Contributor
4392 Points
883 Posts
ASPInsiders
MVP
Re: Html.ActionLink and CSS classes
Apr 10, 2008 01:38 PM|LINK
use new { @class = "classname" }
or new { _class = "classname" }
chrismcabz
Member
5 Points
10 Posts
Re: Html.ActionLink and CSS classes
Apr 10, 2008 01:38 PM|LINK
I've been looking for the answer to this too. In the mean time I'm using IDs, but it's not the best solution.
Html.ActionLink<ControllerName>( c => c.SomeAction(), "item", new { class = "active" }) doesn't work (I guess) because class is a reserved word.
chrismcabz
Member
5 Points
10 Posts
Re: Html.ActionLink and CSS classes
Apr 10, 2008 01:39 PM|LINK
Ah, if only i'd waited a few more seconds...
Thanks a lot!
toinedeboer
Member
21 Points
62 Posts
Re: Html.ActionLink and CSS classes
Apr 10, 2008 02:01 PM|LINK
Tnx.
This works fine but i want to use the Actionlink in an other way.
like: Html.ActionLink("LinkName", "Action", "Controller", new { @class = "active" });
The link link hasnt got a CSS class now but the Class is put as attribute behind the Href url. ???
What do ik do wrong in the syntax ????
tgmdbm
Contributor
4392 Points
883 Posts
ASPInsiders
MVP
Re: Html.ActionLink and CSS classes
Apr 10, 2008 02:14 PM|LINK
eh?
That should output html just like what you wanted in the OP.
Please post the html it generates and the html you want to generate.
Thanks
paul.vencill
Contributor
6716 Points
1358 Posts
Re: Html.ActionLink and CSS classes
Apr 10, 2008 04:41 PM|LINK
I used the lambda syntax and it worked fine:
Html
.ActionLink<HomeController>(h=>h.Index(),"Home",new {@class="active"})using the string-only syntax, it does indeed emit the url with a querystring variable class=active. Smells like a bug to me.
Side by side source:
<div class=SYN_TXT id=syn_row26 style="PADDING-LEFT: 4em; TEXT-INDENT: -0.5em"><LI></div> <div id=syn_block26> <div class=SYN_ROW> <div class=SYN_TXT id=syn_row27 style="PADDING-LEFT: 5em; TEXT-INDENT: -0.5em"><A class="active" href="http://localhost:2222/Home">Home with lambda</A> </div></div></div> <div class=SYN_ROW> <div class=SYN_TXT id=syn_row28 style="PADDING-LEFT: 4em; TEXT-INDENT: -0.5em"></LI></div></div> <div class=SYN_ROW> <div class=SYN_TXT id=syn_row29 style="PADDING-LEFT: 4em; TEXT-INDENT: -0.5em"><LI></div></div> <div id=syn_block29> <div class=SYN_ROW> <div class=SYN_TXT id=syn_row30 style="PADDING-LEFT: 5em; TEXT-INDENT: -0.5em"><A href="http://localhost:2222/Home?class=active">Home with strings</A> </div></div></div> <div class=SYN_ROW> <div class=SYN_TXT id=syn_row31 style="PADDING-LEFT: 4em; TEXT-INDENT: -0.5em"></LI></div></div>Paul
tgmdbm
Contributor
4392 Points
883 Posts
ASPInsiders
MVP
Re: Html.ActionLink and CSS classes
Apr 10, 2008 05:20 PM|LINK
i only ever use the lambda syntax so i never spotted it before.
Yeah, in the string version the object you pass in is the Route Data values. So adding class=active to the querystring is not a bug.
But not having an overload which takes htmlAttributes i think IS a bug.
Well spotted.
Don't forget to mark the posts which helped as answers.