开发者

MVC custom class with generic list in my model

开发者 https://www.devze.com 2023-02-14 03:15 出处:网络
public class ViewModelTables : IEnumerable , IEnumerator { public List<Order> orderList = new List<Order>();
public class ViewModelTables : IEnumerable , IEnumerator
    {
        public List<Order> orderList = new List<Order>();
    }

I have created my own class to strongly type my view. How do I use the genericList of table Order to achieve my task?

public class HomeController : Controller
    {
        public ActionResult Index()
        {
            DataClasses1DataContext dc = new DataClasses1DataContext();
            var my2Tab = new ViewModelTables();
            //my2Tab.customer = (from m in dc.Customers
            //                   where m.CustomerID == "ALFKI"
            //                   select m).First();                        
            var list = (from m in dc.Orders
                     where m.CustomerID == "ALFKI"
                     select m).ToList();
            foreach (va开发者_如何学运维r v in list)
            {
                my2Tab.orderList.Add(v);
            }                      
            return View(my2Tab);
        }

        public ActionResult About()
        {
            return View();
        }
    }

The model item passed into the dictionary is of type 'MvcApplication1.Models.ViewModelTables', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[MvcApplication1.Models.ViewModelTables]'.


Assuming your view is defined to accept the viewmodel like this:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<MvcApplication1.Models.ViewModelTables>" %>

Then I would remove the IEnumerable and IEnumerator from the ViewModelTables class unless you have a specific reason why they are on the class definition.


Right click to Index action select Add View

Click to : Create a Strongly-typed view

Select ViewModelTables model in Model Class Combobox.

Select List from Scaffold template combobox.

Click add.

you can try to remove IEnumerable , IEnumerator from class definition.

0

精彩评论

暂无评论...
验证码 换一张
取 消