ASP.NET MVC3 返回多个实体或泛型

Controller 里面

        public ActionResult Index()

        {

            ViewBag.Message = "欢迎使用 ASP.NET MVC!";

            List<Table01> list = new List<Table01>();

            List<Table02> list2 = new List<Table02>();

            using (TestEntities te = new TestEntities())

            {

                list = (from m in te.Table01

                        select m).ToList<Table01>();

                list2 = (from m in te.Table02

                         select m).ToList<Table02>();

            }



            IDictionary<string, object> test = new Dictionary<string, object>();

            test.Add("T1", list);

            test.Add("T2", list2);



            return View(test);

        }

之后在View里面

@model IDictionary<string, object>

@{

    ViewBag.Title = "主页";

}



<h2>@ViewBag.Message</h2>

<p>

    若要了解有关 ASP.NET MVC 的更多信息,请访问 <a href="http://asp.net/mvc" title="ASP.NET MVC 网站">http://asp.net/mvc</a></p>

@{

    foreach (var item in (List<MvcApplication1.Table01>)Model["T1"])

    {

        <p><a href="@item.ID">@item.Name</a></p>

    }

    }

相应的返回多个实体也是一样的解决办法

你可能感兴趣的:(asp.net)