开发者

Spark : LI Class depndant on IF condition

I'm using MVC3 and Spark.

I need to add a class to a LI if a certain ViewBag element is set to X.

<li id="menu-ho开发者_如何转开发me" class="active?{ViewBag.Active=='home'}" >${Html.ActionLink("Home", "Index", "Site")}</li>

Like the above. This doesnt work, however wondering if there is a way to approach this?


Here are the steps I took and it worked for me:

  1. Create a new ASP.NET MVC 3 project using the default template and the Razor view engine
  2. Install the Spark.Web.Mvc3 NuGet package.
  3. Change the Index action of HomeController to look like this:

    public ActionResult Index()
    {
        ViewBag.Active = "home";
        return View();
    }
    
  4. Rename ~/Views/Home/Index.cshtml to ~/Views/Home/Index.spark and make it look like this:

    <li id="menu-home" class="active?{ViewBag.Active == 'home'}">
        ${Html.ActionLink("Home", "Index", "Site")}
    </li>
    
  5. Run the project

  6. The generated HTML is:

    <li id="menu-home" class="active">
        <a href="/Site">Home</a>
    </li>
    

Remark: Everytime I see someone using ViewBag instead of strongly typed views with view models I feel in the obligation to point this as a bad practice.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜