我正在使用单一视图中的数据库显示多个表
"<pre lang="JavaScript">System.Dynamic.ExpandoObject' does not contain a definition for 'Bus'</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<Bus> Buses { get; set; }
public List<Route> Routes { get; set; }
}
}</pre>
这是控制器
<pre lang="C#">private GpsinformarEntities db = new GpsinformarEntities();
/// Multiple Model in single view using Dynamic View
/// </summary>
/// <returns></returns>
public ActionResult Index()
{
dynamic mymodel = new ExpandoObject();
mymodel.buss = GetBus();
mymodel.Students = GetRoute();
return View(mymodel);
}
public List<Bus> GetBus()
{
List<Bus> buss = new List<Bus>();
return buss = db.Buses.ToList();
}
public List<Route> GetRoute()
{
List<Route> routee = new List<Route>();
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>