我有問題在MVC中發送一個從視圖到控制器的列表。從視圖發送一個列表到控制器的問題MVC
這是鑑於代碼:
foreach (var c in ViewBag.Categories)
{
<a href="@Url.Action("SubCategory", "Product", new { selected = selected, id = @c.Item.ID, category = @c.Item.Name })" id="link" class="list-group-item">
<span> @c.Item.Name</span>
</a>
// { TempData["Childrens"] = c.Children; }
}
的ViewBag.Categories包含兒童名單的每個項目,你可以在所附截圖中看到:
這是控制器中的代碼:
public ActionResult SubCategory(string selected, int id, string category)
{
var childrens = TempData["Childrens"] as IEnumerable<Helpers.TreeItem<Categories>>;
var listOfChildrens = childrens.ToList();
}
我需要在ActionResult的參數列表,孩子列表中,這樣的事情:
public ActionResult SubCategory(string selected, int id, string category, **childrens**)
{
能否請您指點我如何實現這一目標? 在此先感謝!
爲什麼你在使用控制器和TempData的在查看你使用ViewBag? –
TempData僅用於測試 – Orsi
爲什麼不創建** viewmodel **爲這包含選定的,id,類別和categorylist,並傳遞它來查看,而不是使用viewbag – Curiousdev