14

This works:

<li @{if (Model.Mode == "map") {<text> class="bselected"</text>}}>@Html.ActionLink("Map & Directions", MVC.Biz.Show(Model.SingleBiz.BizName, "map", string.Empty))</li>

But it's ugly... Is there a better, cleaner way to do this? in this code, I'm checking if some view data is null or empty, if so add a class.

Or is there another technique to accomplish accomplish this better?

5 Answers 5

15

I posted some Html Extension methods yesterday that handle this kind of thing:

How to concisely create optional HTML attributes with razor view engine?

Using this approach would give you the following Razor syntax:

<li @Html.Css("selected", Model.Mode == "map" )>STUFF</li>

NOTE: you can chain attributes together to build up attribute values based upon multiple conditions. For example:

<li @Html.Css("selected", true).Add("winner", false).Add("last", true)>STUFF</li>

would output:

<li class="selected last">STUFF</li>

Also, if the resultant attribute value is empty the attribute will collapse to keep your html tidy.

Sign up to request clarification or add additional context in comments.

1 Comment

That is very nice, I like that approach - very jQuery like. :D I'll definitely check these out.
12

Or you could do something like this:

@{
    var cssClass = (Model.Mode == "map") ? "selected" : "";
}

<li class="@cssClass">@Html.ActionLink("Map & Directions", MVC.Biz.Show(Model.SingleBiz.BizName, "map", string.Empty))</li>

4 Comments

heh, you'll probably have to change that to var @class=... as class is a keyword. and your li will have to be @(@class)
Ah, ofcourse - I should've seen that! Thanks for pointing that out! Instead, I renamed it to cssClass to keep things looking nice :)
Yeah, that's much better than @(@ :)
Excellent idea, however I would like to avoid having empty class="" in my output in the event it is not the selected item.
3

How about using a ternary operator to evalate an expression as follows:

<li @(Model.Mode == "map" ? "class='bselected' : "")>@Html.ActionLink("Map & Directions", MVC.Biz.Show(Model.SingleBiz.BizName, "map", string.Empty))</li> 

Comments

2

Using a method in the @functions section:

@functions{
   public HtmlString Li(bool selected, IHtmlString template) {
      var result = string.Format("<li{0}>{1}</li>",
         selected ? " class='selected'" : "")),
         template);
      return new HtmlString(result);
   }
}

@* ... *@

@Li(Model.Mode == "map", Html.ActionLink("Map & Directions", MVC.Biz.Show(Model.SingleBiz.BizName, "map", string.Empty)))

Comments

0

I would probably say you could just add the class to your model

<li class="@Model.Selected">@Html.ActionLink("Map & Directions", MVC.Biz.Show(Model.SingleBiz.BizName, "map", string.Empty))</li>

That would clean it up...

Removed second example as I realized it won't work

2 Comments

I like where you're going with this, but I would like to avoid having empty class="" in my output in the event it is not the selected item.
You can use either example provided by Simon or myself to do it...You just supply the Model with your full class definition then or you do what Simon did with the full class definition

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.