我只想替换代码方便Url.Content
而不是使用ViewBag
。我怎么能这样做?
<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
});
答案 0 :(得分:1)
您需要使用以下内容::
<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>
因为您制作的ViewBag包含多个条目(即可枚举)
ViewBag.Menu = BikesDB.ProductSubcategories
.Where(m => m.isSelected == true).AsEnumerable()
.Select(p => new Bike {
Id = p.ProductSubcategoryID,
Name = p.NameofBike
});