我可以通過ViewBag.Categories
將CategoryName字段返回到列表中。我也想返回CategoryId
場,並用它來動態地構建類別詳細查看URL在列表中返回多個對象屬性
這是控制器的動作:
public ActionResult Details(Item item)
{
var db = new appContext();
ViewBag.Item = item.ItemTitle;
ViewBag.ItemLink = "http://localhost:4444/Items/Details/" + item.ItemId;
ViewBag.Categories = new List<string>(item.Categories.Select(c => c.CategoryName));
return View();
}
這是返回類別列表視圖:
@foreach (string category in ViewBag.Categories)
{
<li>
<a href="http://localhost:4444/Categories/Details/???">@category</a>
</li>
}
如何返回CategoryId
如上所示?
我建議你使用@ Html.Action( 「詳細信息」,「類別「,新{ID = category.ID})(基於dotnetoms的答案)創建鏈接。只要您將上面的硬編碼字符串(..//localhost:4444/ ..)發佈到服務器,它就會失敗。同樣'ViewBag.ItemLink應該只是ID。 –