user3046061
user3046061

Reputation: 353

How to add ViewBag property as html class attribute properly?

I have this line:

@Html.ActionLink("Discounts", "ListDiscounts", "Product", null, new { @class = ViewBag.Discount }) 

The ListDiscounts is:

    public ViewResult ListDiscounts(int nrProducts = 5)
    {
        ViewBag.Discount = "selected";
        ProductsListViewModel model = new ProductsListViewModel
        {
            Products = repository.Products
            .Where(p => p.Discount != false)
            .Take(nrProducts)
        };

        return View(model); 
    }

The View that renders the Menu (where my separate Discounts will also be)

@model IEnumerable<string>

@Html.ActionLink("Home", "List", "Product")



@foreach (var link in Model) { 
@Html.RouteLink(link, new
{
  controller = "Product",
  action = "List",
  category = link,
  page = 1
},
new
{
    @class = link == ViewBag.SelectedCategory ? "selected" : null,        
}) 
} 

ListDiscounts.cshtml

@model Sportsstore.WebUI.Models.ProductsListViewModel

@{
   ViewBag.Title = "ListDiscounts";
}

<h2>Discounts available</h2>

@foreach (var p in Model.Products)
{
Html.RenderPartial("ProductSummary", p);
}

I'm trying to add the selected class to my 'a' element in a view but this doesn't work. The ViewBag property remains empty when I click on that Discounts link.

The View associated with ListDiscounts is not the one where that ActionLink line is from (they're separate with the one that has it being a Partial View) but from what I understand ViewBag features have some sort of a global state so this should work?

Any ideas on what is wrong here?

EDIT: Using MVC 4

Upvotes: 1

Views: 2047

Answers (3)

Razack
Razack

Reputation: 950

You just try the below code. Change the Viewbag name and try.

@Html.ActionLink("Discounts", "ListDiscounts", "Product", null, new { @class = ViewBag.CssDiscount })

public ViewResult ListDiscounts(int nrProducts = 5)
{
    ViewBag.CssDiscount = "selected";
    ProductsListViewModel model = new ProductsListViewModel
    {
        Products = repository.Products
        .Where(p => p.Discount != false)
        .Take(nrProducts)
    };

    return View(model); 
}

Upvotes: 0

PaulBinder
PaulBinder

Reputation: 2052

I believe there is something you are not showing us that is the problem. Perhaps you only populated the ViewBag in your post method but not your get method. I created a test application that mocks your app very closely and it works fine.

Controller

namespace MvcApplication1.Controllers
{
    public class HomeController : Controller
    {
        public ViewResult Index()
        {
            ViewBag.Discount = "selected";
            return View();
        }

        [HttpPost]
        public ViewResult Index(int nrProducts = 5)
        {
            var model = new ProductsListViewModel{Products = "stuff"};
            ViewBag.Discount = "selected";
            return View(model);
        }
    }
}

View

@model MvcApplication1.Models.ProductsListViewModel


@Html.ActionLink("Discounts", "Index", "Home", null, new { @class = ViewBag.Discount })
@{ Html.RenderPartial("ViewPage1");}

@using (Html.BeginForm())
{
    <input type="submit" />
}

Partial

@Html.ActionLink("Discounts", "Index", "Home", null, new { @class = ViewBag.Discount })

When I view the source both links have the class I expected. Also after I click on the link they have the class expected. Thus I believe you are not populating the view bag either on the Get and/or the Post controller method

Upvotes: 2

Laziale
Laziale

Reputation: 8225

If you're sure that you have value in the viewbag property instead of @class = ViewBag.Discount try @class = @ViewBag.Discount and see if it works

Upvotes: 0

Related Questions