使用enity frmwork在单个视图中使用数据库显示多个表

时间:2016-10-28 06:29:36

标签: c# entity-framework

我正在使用单一视图中的数据库显示多个表 显示

  

"<pre lang="JavaScript">System.Dynamic.ExpandoObject&#39; does not contain a definition for &#39;Bus&#39;</pre>"

如何解决我的问题?
 这是模特:

<pre lang="C#">public class ViewModel1
   {
       public class Bus
       {
           public int Id { get; set; }
           public string BusName { get; set; }

       }

       public class Route
       {
           public int RouteId { get; set; }
           public string RouteNo { get; set; }
       }

       public class MyViewModel
       {
           public List&lt;Bus&gt; Buses { get; set; }
           public List&lt;Route&gt; Routes { get; set; }
       }

   }</pre>

这是控制器

<pre lang="C#">private GpsinformarEntities db = new GpsinformarEntities();
        /// Multiple Model in single view using Dynamic View
        /// &lt;/summary&gt;
        /// &lt;returns&gt;&lt;/returns&gt;
        public ActionResult Index()
        {

            dynamic mymodel = new ExpandoObject();
            mymodel.buss = GetBus();
            mymodel.Students = GetRoute();
            return View(mymodel);
        }


        public List&lt;Bus&gt; GetBus()
        {
            List&lt;Bus&gt; buss = new List&lt;Bus&gt;();
            return buss = db.Buses.ToList();

        }

        public List&lt;Route&gt; GetRoute()
        {
            List&lt;Route&gt; routee = new List&lt;Route&gt;();
            return routee = db.Routes.ToList();
        }
    }</pre>

这是View

@using new_app.Models;
@model dynamic
@{
    ViewBag.Title = "Home Page";
}
<h2>@ViewBag.Message</h2>

<p><b>Bus List</b></p>

<table>
    <tr>

        <th>BusName</th>
    </tr>
    @foreach (Bus item in Model.Bus)
    {
        <tr>
            <td>@item.BusName</td>

        </tr>
    }
</table>

<p><b>Route list</b></p>

<table>
    <tr>

        <th>Route</th>

    </tr>
    @foreach (var item in Model.Route)
    {
        <tr>
            <td>@item.RouteNo</td>

        </tr>
    }
</table>

0 个答案:

没有答案