我想显示viewbag列表

时间:2015-10-12 16:31:48

标签: c# asp.net-mvc asp.net-mvc-4 razor

在我的课程菜单代码和说明名称

namespace Test.Controllers
{
class menus 
   {
    public string Idmenus { get; set; }

    public string desname { get; set; }  
    }
}

在我的控制器中我想将数据添加到对象列表

  var viewdes = db.menudescriptions.Where(w => w.Idmenu == mids).ToList();
  var desc = new List<menus>();     
  foreach (var b in viewdes) {
           desc.Add(new menus { Idmenus = b.Iddesmenu });
           desc.Add(new menus { desname = b.descriptionname });
       }
       if ((desc.Count != 0))
       {
           ViewBag.Id = desc.ToList();
       }

我想向{View 1}显示ViewBag.Id

 @if (ViewBag.Id != null)
        {
            <td>
                @foreach (var per in `enter code here`)
                {

         @Html.ActionLink(@per.ToString(), "detail", new { mides = per })

                }
            </td>

        }

1 个答案:

答案 0 :(得分:0)

你基本上只需要像使用foreach时通常那样迭代你的ViewBag.Id

@foreach(var per in ViewBag.Id as List<menus>)
{
   @Html.ActionLink(per.desname, "detail", new { mides = per })
}