C# 如何在Asp.net MVC中联接两个表并将数据显示到表中

C# 如何在Asp.net MVC中联接两个表并将数据显示到表中,c#,entity-framework,C#,Entity Framework,我在asp.netmvc中创建了一个简单的汽车零售系统。我想连接两个表,并在asp.netmvc中将数据显示到表中。我不知道如何将数据合并并显示到表中。我到目前为止尝试的内容附在下面 车型 public partial class carreg { public int id { get; set; } public string carno { get; set; } public string make { get; set; }

我在asp.netmvc中创建了一个简单的汽车零售系统。我想连接两个表,并在asp.netmvc中将数据显示到表中。我不知道如何将数据合并并显示到表中。我到目前为止尝试的内容附在下面

车型

 public partial class carreg
    {
        public int id { get; set; }
        public string carno { get; set; }
        public string make { get; set; }
        public string model { get; set; }
        public string Available { get; set; }
    }
public partial class rentail
    {
        public int id { get; set; }
        public string carno { get; set; }
        public Nullable<int> custid { get; set; }
        public Nullable<int> fee { get; set; }
        public Nullable<System.DateTime> sdate { get; set; }
        public Nullable<System.DateTime> edate { get; set; }

        public virtual rentail rentail1 { get; set; }

}
  id  carno   custid     fee      sdate       edate
   3    A0001    1      3434      2019-12-09  2019-12-27
   4    A0002    1      12000     2019-11-01  2019-11-29
租赁车型

 public partial class carreg
    {
        public int id { get; set; }
        public string carno { get; set; }
        public string make { get; set; }
        public string model { get; set; }
        public string Available { get; set; }
    }
public partial class rentail
    {
        public int id { get; set; }
        public string carno { get; set; }
        public Nullable<int> custid { get; set; }
        public Nullable<int> fee { get; set; }
        public Nullable<System.DateTime> sdate { get; set; }
        public Nullable<System.DateTime> edate { get; set; }

        public virtual rentail rentail1 { get; set; }

}
  id  carno   custid     fee      sdate       edate
   3    A0001    1      3434      2019-12-09  2019-12-27
   4    A0002    1      12000     2019-11-01  2019-11-29
rentail

 public partial class carreg
    {
        public int id { get; set; }
        public string carno { get; set; }
        public string make { get; set; }
        public string model { get; set; }
        public string Available { get; set; }
    }
public partial class rentail
    {
        public int id { get; set; }
        public string carno { get; set; }
        public Nullable<int> custid { get; set; }
        public Nullable<int> fee { get; set; }
        public Nullable<System.DateTime> sdate { get; set; }
        public Nullable<System.DateTime> edate { get; set; }

        public virtual rentail rentail1 { get; set; }

}
  id  carno   custid     fee      sdate       edate
   3    A0001    1      3434      2019-12-09  2019-12-27
   4    A0002    1      12000     2019-11-01  2019-11-29
我想查看如下所示的数据

   id    carno  custid   fee      sdate       edate       Available  
   3   A0001     1      3434      2019-12-09  2019-12-27    yes
   4   A0002     1      12000     2019-11-01  2019-11-29    no
RentController

public ActionResult Index()
        {
            return View(db.rentails.ToList());
        }
进入视野

@model IEnumerable<WebApplication36.Models.rentail>

  <table class="table">
            <tr>
                <th>
                    Carno
                </th>
                <th>
                    Custid
                </th>
                <th>
                    Fee
                </th>
                <th>
                    Start date
                </th>
                <th>
                    End date
                </th>
            </tr>

            @foreach (var item in Model)
            {
                <tr>
                    <td>
                        @item.carno
                    </td>
                    <td>
                        @item.custid
                    </td>
                    <td>
                        @item.fee
                    </td>
                    <td>
                        @item.sdate
                    </td>
                    <td>
                       @item.edate
                    </td> 
                </tr>
            }

        </table>
@model IEnumerable
卡诺
卡斯蒂德
费用
开始日期
结束日期
@foreach(模型中的var项目)
{
@项目1.carno
@item.custid
@项目.费用
@item.sdate
@项目1.edate
}

在此表中,我想添加另一个可用列如何连接can表和retail以获取数据首先,您应该创建一个类ViewModel:包含您想要的任何属性。 例如:

public class RentailViewModel
{
    public int RentalId { get; set; }

     public string carno { get; set; }
     public Nullable<int> custid { get; set; }
     public Nullable<int> fee { get; set; }
     public Nullable<System.DateTime> sdate { get; set; }
     public Nullable<System.DateTime> edate { get; set; }

     public string Available { get; set; }

    // The rest of properties
}

已成功显示租赁数据。但我不认为ASP.NETMVC是一个web框架,而不是一个数据访问库。它不查询数据库。您正在使用实体框架吗?哪个版本?在所有的ORM中,ORM的任务是从实体之间的关系生成连接。如果必须写入联接,则实体配置不正确。您使用的是哪个EF版本?5.6.6.2? EF Core?custid=r.custid,fee=r.fee,sdate=r.sdate,edate=r.edate,这些属性获取的错误不能隐式转换类型int?若要执行int和显式转换,请在RentailViewModelRentailViewModel中更新其余属性i已更新,但custid=r.custid,fee=r.fee,sdate=r.sdate,edate=r.edate,这些属性获取错误不能隐式转换int类型?到int和显式转换exists@Javafiver不是真的-没有理由在ORMs中使用连接。ORM根据对象之间的关系生成连接。如果必须使用联接,则会出现问题
Rental
也应该有一个
Car
propertyPls更新名称空间,现在它是@model IEnumerable