在MVC 2中查看数据无法正常工作

时间:2014-07-11 10:52:52

标签: c# asp.net asp.net-mvc visual-studio asp-classic

嗯,我对mvc很新,我在这里遇到了一个问题。我想使用ViewData将动作方法中的数据传递给我的视图。我在Visual Studio 2008中使用了Asp.net MVC 2.0。我知道如何使用动作方法中的视图数据&在视图中使用它。

Here is what i have written in my action method : - 

             public ActionResult Index()
                    {
                        NorthWindEntities db = new NorthWindEntities();
                        var data = (from id in db.Orders
                                    join ordid in db.Order_Details on id.OrderID equals                  ordid.OrderID
                                    select new
                                    {
                                        orderid = id.OrderID,
                                        productid = ordid.ProductID,
                                        discount = ordid.Discount
                                    }
                                      );
                        ViewData["Data"] = data;
                        return View();
                    }

以下是视图中的代码: -

        <%foreach (var item in (IEnumerable)ViewData["Data"] )
          {
              Response.Write(item.orderId);

          } %>

它给出了这样的错误..

  

'object'不包含'orderId'的定义,也没有扩展名   方法'orderId'接受'object'类型的第一个参数可以是   发现(您是否缺少using指令或程序集引用?)

提前致谢

2 个答案:

答案 0 :(得分:2)

尝试以下(使用viewmodel的最佳做法...创建一个OrderViewModel类,然后使用您选择的新OrderViewModel填充它,就像这样...

namespace MyApp.ViewModels
{
    public class OrderViewModel
    {
        public int OrderId { get; set; }

        public int ProductId { get; set; }

        public int Discount { get; set; }
    }
}

public ActionResult Index()
{
    NorthWindEntities db = new NorthWindEntities();

    List<OrderViewModel> data = (from id in db.Orders
                                join ordid in db.Order_Details on id.OrderID equals ordid.OrderID
                                select new OrderViewModel()
                                {
                                    OrderId = id.OrderID,
                                    ProductId = ordid.ProductID,
                                    Discount = ordid.Discount
                                }).ToList();

    ViewData["Data"] = data;
    return View();

然后在你的视图页面中......

@using MyApp.ViewModels

<%foreach (var item in (List<OrderViewModel>)ViewData["Data"] )
{
    Response.Write(item.orderId);
}%>

答案 1 :(得分:1)

您需要采用以下一种视图模型: -

public class ViewModelClass
{
     public int OrderId{ get; set; }
     public int ProductId{ get; set; }
     public int Discount{ get; set; }
}

并在控制器中: -

  public ActionResult Index()
                    {
                        NorthWindEntities db = new NorthWindEntities();
                        var data = (from id in db.Orders
                                    join ordid in db.Order_Details on id.OrderID equals                  ordid.OrderID
                                    select new ViewModelClass
                                    {
                                        orderid = id.OrderID,
                                        productid = ordid.ProductID,
                                        discount = ordid.Discount
                                    }
                                      );
                        ViewData["Data"] = data;
                        return View();
                    }

并在视图中: -

 <%foreach (var item in (ViewData["Data"] as List<ViewModelClass>))
          {
              Response.Write(item.orderId);

          } %>