如何在Asp.net MVC中获取viewbag数据?

时间:2012-10-30 14:53:26

标签: asp.net-mvc-3

如何获取viewbag数据,我创建了一个控制器,该数据是从数据库中获取并尝试在视图中显示数据,但它不显示数据。

  public ActionResult Show()
    {
        List<Models.Employees> lst = new List<Models.Employees>();
        Models.Employees obj = new Models.Employees();
        DataSet ds =new Models.BL.InsertBL().GetShow(obj);
        lst = (from DataRow drw in ds.Tables[0].Rows
               select new Models.Employees
               {
                   Enames = drw["Ename"].ToString(),
                   DepartId =Convert .ToInt32(drw["Departmentid"]),
                   EmailIds = drw["Emailid"].ToString(),
                   Adress = drw["Address"].ToString()
               }).ToList();
        ViewBag.Sasi = lst; 
        return View();
    }

   @{
    ViewBag.Title = "Show";
     Layout = "~/Views/Shared/VideoLayout.cshtml"; 
      var vv = ViewBag.Sasi;    
    }

1 个答案:

答案 0 :(得分:3)

在这种情况下,我建议你强烈输入视图:

return View(lst);

并在视图中:

@model List<Models.Employees>

你的问题,试试这个:

(List<Models.Employees>)ViewBag.Sasi