Controller  Action方法:
public ActionResult Index(int postID)
        {
            ClassBlogDBDataContext cb = new ClassBlogDBDataContext();
            ViewData["post"] =(IQueryable<T_Post>) cb.T_Post.SingleOrDefault(p => p.PostID == postID);
            return View("Index");
        }传递一个ViewData["post"]  
在index 中
   <% foreach (var post in ViewData["post"] as List<T_Post>)
           { %>出现错误
未将对象引用设置到对象的实例。ViewData["post"] as List<T_Post> 转换失败!!! 为null试了许多页用VIEWDATA方法传递值转为强类型的就会失败  ...何解????

解决方案 »

  1.   

    通过 RenderView() 方法 来实现强类型类传递ViewData参考:
      

  2.   

    用強類型類來會传递可以实现,这个方法我试过。。   目前在学习MVC。。所以想用不同的方法来做下。。!!就发现VIEWDATA里是有数据。。但转换就出错!!
      

  3.   

    先转换 判断为空   <%  var list=ViewData["post"] as List<T_Post>;
    if(list!=null)
    {
            foreach (var post in list)
               { %>
      

  4.   

    现在的问题是有值 ViewData["post"] 为null
      

  5.   

    ViewData["list"] = (from c in db.News select c).ToList();/ToArray();
    这个只是一个声名, .ToList();/ToArray();才是取出数据了
    这样理解有问题么.