So in diagram of edmx file two entities will display. and Model.Context.cs files look like ,
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated from a template.
//
// Manual changes to this file may cause unexpected behavior in your application.
// Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace EFDatabaseFirstApproch.Models
{
using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
public partial class ProductEntities : DbContext
{
public ProductEntities()
: base("name=ProductEntities")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public virtual DbSet<Orders> Orders { get; set; }
public virtual DbSet<Product> Product { get; set; }
}
}
public ActionResult Index() { ProductEntities db = new ProductEntities(); var prdData = from prd in db.Product join ord in db.Orders on prd.ProductId equals ord.ProductId select new { ProductId = prd.ProductId, ProductName = prd.ProductName, OrderId = ord.OrderId }; return View(prdData.ToList()); }
I want view like this,
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<table>
<tr>
<th>
Product Id
</th>
<th>
Product Name
</th>
<th>
Order Id
</th>
<th></th>
</tr>
@foreach (var item in ViewData.Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.ProductId)
</td>
<td>
@Html.DisplayFor(modelItem => item.ProductName)
</td>
<td>
@Html.DisplayFor(modelItem => item.OrderId)
</td>
</tr>
}
</table>
public ActionResult Index()
{
ProductEntities db = new ProductEntities();
var prdData = db.Orders.Include(o=>o.Product)
return View(prdData.ToList());
}
Update View(Strongly Typed)
@model IEnumerable<EFDatabaseFirstApproch.Models.Order>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<table class="table">
<tr>
<th>
Product Id
</th>
<th>
Product Name
</th>
<th>
Order Id
</th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.ProductId)
</td>
<td>
@Html.DisplayFor(modelItem => item.Product.ProductName)
</td>
<td>
@Html.DisplayFor(modelItem => item.OrderId)
</td>
</tr>
}
</table>
Create a ViewModels folder in your solution and create a ViewModel class and add properties of both class that you need in View :
public class OrderVM
{
public int ProductId {get;set;}
public string ProductName {get;set;}
public int OrderId {get;set; }
}
then in your action create it this way:
public ActionResult Index()
{
ProductEntities db = new ProductEntities();
var prdData = from prd in db.Product
join ord in db.Orders on prd.ProductId equals ord.ProductId
select new OrderVM
{
ProductId = prd.ProductId,
ProductName = prd.ProductName,
OrderId = ord.OrderId
};
return View(prdData.ToList());
}
View:
@{
ViewBag.Title = "Index";
}
@model NameSpace.ViewModels.OrderVM
<h2>Index</h2>
<table>
<tr>
<th>
Product Id
</th>
<th>
Product Name
</th>
<th>
Order Id
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.ProductId)
</td>
<td>
@Html.DisplayFor(modelItem => item.ProductName)
</td>
<td>
@Html.DisplayFor(modelItem => item.OrderId)
</td>
</tr>
}
</table>
Member
108 Points
113 Posts
Display data from two tables and show it in page(.CSHTML) using EF Designer from Database
Oct 31, 2014 04:33 AM|rjpithwa|LINK
Hi,
I am using EF Designer from Database in mu web application.
I have two tables like,
So in diagram of edmx file two entities will display. and Model.Context.cs files look like ,
Connection String is like,
Controller :
I want view like this,
How can I get both tables data in view ?
All-Star
37441 Points
9078 Posts
Re: Display data from two tables and show it in page(.CSHTML) using EF Designer from Database
Oct 31, 2014 05:15 AM|AidyF|LINK
https://www.google.com/search?q=site%3aforums.asp.net+two+models
Member
110 Points
28 Posts
Re: Display data from two tables and show it in page(.CSHTML) using EF Designer from Database
Oct 31, 2014 06:06 AM|avishek.kumar66|LINK
Update Controller:
Update View(Strongly Typed)
pls mark as "Answered Question" if it is helpful.
Thanks,
Avishek
mvc
Member
420 Points
116 Posts
Re: Display data from two tables and show it in page(.CSHTML) using EF Designer from Database
Oct 31, 2014 02:37 PM|ehsansajjad465|LINK
Create a ViewModels folder in your solution and create a ViewModel class and add properties of both class that you need in View :
then in your action create it this way:
View:
model mvc