Question

All I want to replace code handy Url.Content instead of using ViewBag. How could I do that?

<a href="@Url.Content("~/Bike/")">Home</a>
   <ul>
       <li>
           <a href="@Url.Content("~/Bike/Categories/1?name=Mountain Bikes&class=image")">Mountain Bikes</a>
       </li>
       <li>
           <a href="@Url.Content("~/Bike/Categories/2?name=Road Bikes&class=image")">Road Bikes</a>
       </li>
       <li>
           <a href="@Url.Content("~/Bike/Categories/3?name=Touring Bikes&class=image")">Touring Bikes</a>
       </li>

   </ul>

ViewBag:

 ViewBag.Menu = BikesDB.ProductSubcategories
                .Where(m => m.isSelected == true).AsEnumerable()
                .Select(p => new Bike { 
                                        Id = p.ProductSubcategoryID,
                                        Name = p.NameofBike 
                         });
Was it helpful?

Solution

You need to use the Following for that ::

<a href="@Url.Content("~/Bike/")">Home</a>
<ul>


    @foreach (var item in @ViewBag.Menu)
    {
       <li>
          <a href="@Url.Content("~/Bike/Categories/"+item.ID+"?name="+item.Name+"&class=image")">@item.Name</a>
       </li>   
    }

</ul>

Because the ViewBag you have made contains multiple Entries(i.e. Enumerable)

ViewBag.Menu = BikesDB.ProductSubcategories
                .Where(m => m.isSelected == true).AsEnumerable()
                .Select(p => new Bike { 
                                        Id = p.ProductSubcategoryID,
                                        Name = p.NameofBike 
                         });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top