Linq Join Query - 如何在视图中显示新表MVC C# [英] Linq Join Query - How to display new table in view MVC C#
本文介绍了Linq Join Query - 如何在视图中显示新表MVC C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在linq中添加了一个查询来加入2个类,问题现在我想在视图中显示它,因为查询在控制器中是查询:
< pre $
public ActionResult JoinSupToPro()
{
SupplierDBContext dbS = new SupplierDBContext();
var innerJoinQuery =从db.Products中的pro加入到proS.Suppliers上的sup中sup。suppplierId等于sup.ID
选择新的{pro.Name,pro.Price,NameSup = sup.Name,sup。电话,};
return View();
}
如何在视图中显示它们?
解决方案
我建议创建一个新的 viewmodel
Class来从2个不同的表如下:
public class ProductSupplier
{
public string ProName {get; set;}
public string Price {get; set;}
public string SupName {get; set;}
public string SupPhone {get; set;}
}
现在,当从控制器返回数据
填写模型
,并返回查看如下:
public ActionResult JoinSupToPro()
{
SupplierDBContext dbS = new SupplierDBContext();
//创建您的模型的列表实例
List< ProductSupplier> model = new List< ProductSupplier>();
var innerJoinQuery =(来自dbs.Products中的pro
加入dbS.Suppliers中的sup
上pro.SupplierId等于sup.ID
选择新的
{
proName = pro.Name,
proPrice = pro.Price,
supName = sup.Name,
supPhone = sup.Phone
})。ToList(); // convert以列出
foreach(var item in innerJoinQuery)//检索每个项目并分配给模型
{
model.Add(new ProductSupplier()
{
ProName = item.proName,
Price = item.proPrice,
SupName = item.supName,
SupPhone = item.supPhone
});
}
return View(model);
$ b 一旦从控制器传递模型,您可以在视图中显示它,如下所示: / b>
//在您的视图中引用模型的列表实例
@model IEnumerable< ProjectName.Models.ProductSupplier>
@foreach(模型中的var项)
{
//将此值赋给html
的任何元素//将其引用为@ item.ProName,@ item.Price等等,
}
I made a query in linq to join 2 classes the problem is now i want to show it in the view because the query was in the controller here is the query:
public ActionResult JoinSupToPro()
{
SupplierDBContext dbS = new SupplierDBContext();
var innerJoinQuery = from pro in db.Products join sup in dbS.Suppliers on pro.SupplierId equals sup.ID
select new { pro.Name,pro.Price, NameSup= sup.Name , sup.Phone,};
return View();
}
How do i show them now in th view?
解决方案 I would suggest to create a new viewmodel
Class to load data from 2 different tables like one below:
public class ProductSupplier
{
public string ProName {get; set;}
public string Price{get; set;}
public string SupName{get; set;}
public string SupPhone{get; set;}
}
Now when returning data from your controller
you fill the model
and return it to view as below:
public ActionResult JoinSupToPro()
{
SupplierDBContext dbS = new SupplierDBContext();
//Create list instance of your model
List<ProductSupplier> model=new List<ProductSupplier>();
var innerJoinQuery = (from pro in dbs.Products
join sup in dbS.Suppliers
on pro.SupplierId equals sup.ID
select new
{
proName=pro.Name,
proPrice=pro.Price,
supName= sup.Name ,
supPhone=sup.Phone
}).ToList();//convert to List
foreach(var item in innerJoinQuery) //retrieve each item and assign to model
{
model.Add(new ProductSupplier()
{
ProName = item.proName,
Price = item.proPrice,
SupName = item.supName,
SupPhone = item.supPhone
});
}
return View(model);
}
Once model is passed from controller you can display it in view as below:
//Refer the list instance of model in your view
@model IEnumerable<ProjectName.Models.ProductSupplier>
@foreach(var item in Model)
{
//assign this values to any element of html
//by referring it as @item.ProName, @item.Price etc.,
}
Hope it's clear
这篇关于Linq Join Query - 如何在视图中显示新表MVC C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文