使用实体框架在MVC中使用多个表 [英] Working with multiple tables in MVC using entity framework

查看:81
本文介绍了使用实体框架在MVC中使用多个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

对接显示

 System.Dynamic.ExpandoObject ' 不包含 Bus '的定义  





如何解决我的问题

这里是Model

  public   class  ViewModel1 
{
public class 巴士
{
public int ID { get ; set ; }
public string BusName { get ; set ; }

}

public class 路线
{
public int RouteId {获得; set ; }
public string RouteNo { get ; set ; }
}

public class MyViewModel
{
public 列表<总线>公共汽车{获取; set ; }
public 列表< Route>路线{获取; set ; }
}

}





这里是控制器

< br $> b $ b

  private  GpsinformarEntities db =  new  GpsinformarEntities(); 
/// 使用动态视图的单一视图中的多个模型
/// < / summary >
/// < 返回 > < / returns >
public ActionResult Index()
{

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


public 列表<总线> GetBus()
{
List< Bus> buss = new List< Bus>();
return buss = db.Buses.ToList();

}

public 列表< Route> GetRoute()
{
List< Route> routee = new 列表< Route>();
return routee = db.Routes.ToList();
}
}







这里是查看



 @ using new_app.Models; 
@model 动态
@ {
ViewBag.Title = 主页;
}
< h2> @ ViewBag.Message < / h2 < span class =code-keyword>>

< p>< b>总线列表< / b > < / p >

< table>
< tr>

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

< / tr >
}
< / 表格 >

< p>< b>路线列表< / b > < ; / p >

< table>
< tr>

< th>路线< / th >

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

< / tr >
}
< / >


单个视图中的




我尝试了什么:



我可以使用显示多个表格来自数据库 单视图
butt显示
< pre lang = JavaScript > System.Dynamic.ExpandoObject&#39;不包含&#39; Bus&#39;< / pre> $ b $的定义b
如何解决我的问题

解决方案

您错过了视图的重要部分 - 生成的部分错误。



如果你仔细观察你传递给视图的模型,你会发现它不包含一个名为<$ c的属性$ C>总线。相反,它包含一个名为 buss 的属性,其中包含 Bus 对象的列表。



所以在你看来,你有一些看起来像:

 @foreach(Model.Bus中的ViewModel1.Bus总线)



您需要更改它以使用正确的属性名称:

 @foreach(Model.buss中的ViewModel1.Bus总线) )


i ma working with display multiple tables from database in single view
butt show the
"

System.Dynamic.ExpandoObject' does not contain a definition for 'Bus'

"

how to solve my problem
here is Model

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; }
       }

   }



here is Controller


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();
        }
    }




here is 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>


in single view

What I have tried:

i ma working with display multiple tables from database in  single view 
butt show the 
"<pre lang="JavaScript">System.Dynamic.ExpandoObject&#39; does not contain a definition for &#39;Bus&#39;</pre>"

how to solve my problem

解决方案

You've missed out the important part of your view - that part that generates the error.

If you look closely at the model you're passing to the view, you'll see that it doesn't contain a property called Bus. Instead, it contains a property called buss, which contains a list of Bus objects.

So somewhere in your view, you have something that looks like:

@foreach (ViewModel1.Bus bus in Model.Bus)


You need to change that to use the correct property name:

@foreach (ViewModel1.Bus bus in Model.buss)


这篇关于使用实体框架在MVC中使用多个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆