2

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?

2D3D
  • 353
  • 1
  • 6
  • 22

3 Answers3

7

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

Guruprasad J Rao
  • 29,410
  • 14
  • 101
  • 200
  • I did exactly as you mentioned yet im getting this error :The specified LINQ expression contains references to queries that are associated with different contexts. – 2D3D Jul 01 '15 at 13:44
  • @2D3D Does your `SupplierDBContext` contains `Products` table? If yes then I've updated by answer, it should have been `dbs.Products` in `linq` query and if no then you need to keep it in same context! – Guruprasad J Rao Jul 01 '15 at 13:56
  • I dont have a product table in supplierDbContext is there no other way to fix this? @Guruprasad Rao – 2D3D Jul 01 '15 at 14:06
  • I ain't sure about this!! But this answer **[here](http://stackoverflow.com/a/2509824/2065039)** might help you!! Or may be this **[answer](http://stackoverflow.com/a/898539/2065039)** too – Guruprasad J Rao Jul 01 '15 at 14:14
0

You have two options here, one use Ajax to display this query in the view. Useful, but you may need to create html dynamically, depends on what you want to achieve.

The second one is to use view model and to call this model in your view, so will be able to achieve the following:

foreach(var item in model) 
{
<td>item.prop<td>
}

Good Luck!

Barr J
  • 10,636
  • 1
  • 28
  • 46
0

You Need to Pass data to view. like

return view(innerJoinQuery.ToList())

Please Refer this link for Details

http://www.asp.net/mvc/overview/getting-started/introduction/accessing-your-models-data-from-a-controller