0

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

class diagram

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

Out Put

7
  • grid.Column("ShapeName", header: "Shape Name " + Html.SortDirection(ref grid, "Shape")@item.Shape.Name) Commented Jan 30, 2016 at 11:10
  • StoreBrowseViewModel does not have valves Shape Name and clarity name only id Commented Jan 30, 2016 at 11:12
  • It should. That's the whole point of the viewModel. You send whatever data you want. Commented Jan 30, 2016 at 11:13
  • This is question I have my view model not pass shape name and clarity name. var viewModel = new StoreBrowseViewModel(). this is my chtml page grid.Column("ShapeId", header: "Shape " + Html.SortDirection(ref grid, "Shape")) Commented Jan 30, 2016 at 11:17
  • If I add Gemstones single table all fields including Shape name and Clarity name, It'll display all in my chtml page. Commented Jan 30, 2016 at 11:26

1 Answer 1

1

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>
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

StoreBrowseViewModel has only public IEnumerable<Gemstone> Gemstones { get; set; }
I am getting @item.Shape.Name it say {"'System.Web.Helpers.WebGridRow' does not contain a definition for 'Shape'"}
It say {"Column \"Shape\" does not exist."} why was that? grid.Column("Shape", header: "Shape " + Html.SortDirection(ref grid, "Shape")),

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.