简体   繁体   中英

How to MVC C# Viewmodel to join data from multiple tables

Can anyone please tell me, I need to MVC C# Viewmodel join data from multiple tables and use chtml page @model ViewModels.StoreBrowseViewModel. But my logic will retrieve only one table data. This is my class diagram. red box primary key, blue box foreign key

类图

This is my StoreBrowseViewModel class

 public class StoreBrowseViewModel
 {

        public int Id { get; set; }
        public string Shape { get; set; }
        public string Name { get; set; }
        public string Clarity { get; set; }
        public int CategoryID { get; set; }
        public Category Category { get; set; }
        public Shape Shapes { get; set; }
        public IEnumerable<Gemstone> Gemstones { get; set; }
        public IEnumerable<Clarity> Clarites { get; set; }
}

This is my action method.

 public ActionResult Browse(string gemCategory = "")
 {

  var gemstones = from g in db.Gemstones
                            select g;
var category = db.Categories.Where(p => p.Name == gemCategory).FirstOrDefault();
 gemstones = (gemstones.Include(s => s.Shapes)
                    .Include(c => c.Clarities)
                    .Where(p => p.CategoryID == category.CategoryID)); 
 var viewModel = new StoreBrowseViewModel()            
            {
                Category = category,
                Gemstones = gemstones,
            };
 return this.View(viewModel);
}

This is my view model chtml page

@model ViewModels.StoreBrowseViewModel
grid.Column("Carat", header: "Weight " + Html.SortDirection(ref grid, "Carat")@item.Carat),
grid.Column("ShapeId", header: "Shape " + Html.SortDirection(ref grid, "Shape")@item.Shape),
grid.Column("ClarityId", header: "Clarity " + Html.SortDirection(ref grid, "Clarity")@item.Clarity),
grid.Column("Price", header: "Price(USD) " + Html.SortDirection(ref grid, "Price")@item.Price),

This is my out put It should display shape name and clarity name

输出

I would do it differently from what im gona show below but this should help...

public ActionResult Browse(string gemCategory = "")
{
    var category = db.Categories.FirstOrDefault(p => p.Name == gemCategory);

    var gemstones = db.Gemstones.Include(s => s.Shapes)
                                .Include(c => c.Clarities)
                                .Include(c => c.Categories)
                                .Include(c => c.Cuts)
                                .Include(c => c.Orgins)
                                .Where(p => p.CategoryID == category.CategoryID);

    var viewModel = new StoreBrowseViewModel() {Gemstones = gemstones};

    return View(viewModel);
}

view model

public class StoreBrowseViewModel
{
    public IEnumerable<Gemstone> Gemstones { get; set; }
}

in the view

@foreach(var item in Model.Gemstones)
{
    <span>@item.Name</span>

    @foreach(var item2 in Model.Gemstones.Clarities)
    {
        <span>@item2.Name</span>
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM