First Last

First Last

  • 1k
  • 648
  • 67.4k

ASP.Net MVC and using Linq to Sql

Jan 9 2018 6:44 PM
I'm following an MVC 5 tutorial: http://www.c-sharpcorner.com/article/dashboard-application-with-asp-net-mvc-5-and-jquery/
 
but the Author left out 1 of the features - the middle panel - "Orders" (which is the orders for all customers). It has a View Details link but no code and partial view is displayed when clicked.
 
So I'm trying to do create that partial view but having trouble with writing the Linq To Sql.
I am trying for just a partial view that receives a flattened list (CustomerName, CustomerImage and possible OrderDate will repeat):
 
CustName CustomerImage OrderDate ProductTpe ProductName ProductImage Quantity
CustName CustomerImage OrderDate ProductTpe ProductName ProductImage Quantity
 
Here is the ViewModel I created to represent the above:
 
public class CustomerOrdersViewModel
{
   public string CustomerName { get; set; }
   public string CustomerImage { get; set; }
   public DateTime OrderDate { get; set; }
   public string ProductType { get; set; }
   public string ProductName { get; set; }
   public string ProductImage { get; set; }
   public int Quantity { get; set; }
}
 
Here is the DbContext and the models that the Author created:
 
DbContext:
 
public class DashboardContext : DbContext
{
   // Constructor - inherits the base constructor.
   public DashboardContext() : base("DashboardOrder")
   { 
   }
 
   public IDbSet CustomerSet { get; set; }
   public IDbSet OrderSet { get; set; }
   public IDbSet ProductSet { get; set; }
   public IDbSet OrderDetailSet { get; set; }
}
 
Customer:
 
public class Customer : IEntity
{
   public Customer()
   {
      Orders = new List();
   }
 
   public int ID { get; set; }
   public string CustomerName { get; set; }
   public string CustomerEmail { get; set; }
   public string CustomerPhone { get; set; }
   public string CustomerCountry { get; set; }
   public string CustomerImage { get; set; }
 
   public virtual ICollection Orders { get; set; }
}
 
IEntity:
 
public interface IEntity
{
   int ID { get; set; }
}
 
Order:
 
public class Order : IEntity
{
   public Order()
   {
     OrderDetail = new List();
   }
 
   public int ID { get; set; }
   public DateTime OrderDate { get; set; }
   public virtual Customer Customer { get; set; }
   public virtual ICollection OrderDetail { get; set; }
}
 
OrderDetails:
 
public class OrderDetails : IEntity
{
   public int ID { get; set; }
   public int Quatity { get; set; }  
 
   public virtual Order Order { get; set; }
   public virtual Product Product { get; set; }
}
 
Product:
 
public class Product : IEntity
{
   public Product()
   {
     OrderDetails = new List();
   }
   public int ID { get; set; }
   public string ProductName { get; set; }
   public decimal UnitPrice { get; set; }
   public int UnitsInStock { get; set; }
   public string ProductImage { get; set; }
   public string ProductType { get; set; }
 
   public virtual ICollection OrderDetails { get; set; }
}
 
Here is the Action Method in the DashboardController I was attempting to write.
 
I believe I want to read the Order Model which in turn has reference to the Customer and a list of Orders which in turn has a reference to the list of OrderDetail which has reference to the Product.
 
But I'm having a hard time with the "Linq to SQL" to get the data in the lists in the models to create my ViewModel to flatten it out and pass to my partial view as a list.
 
public ActionResult GetCustomerOrdersDetails(string type)
{
   List customerOrders = null;
 
   using (DashboardContext _context = new DashboardContext())
   {
       // Using LINQ TO SQL and deferred execution via the "ToList".
      customerOrders = (from o in _context.OrderSet
                                    select new CustomerOrdersViewModel
                                    {
                                      CustomerName = o.Customer.CustomerName,
                                      CustomerImage = o.Customer.CustomerImage,
                                      OrderDate = o.OrderDate,
                                      -- Here I need to process the list of Orders which in turn has a
                                      -- reference to the list of OrderDetail which has
                                      -- reference to the Product.
                                      ProductType = ?,
                                      ProductName = ?,
                                      ProductImage = ?,
                                     Quantity = ?,
                                   }).ToList();
  }
  
     return PartialView("~/Views/Dashboard/GetCustomerOrdersDetails.cshtml", 
     customerOrders);
}

Answers (2)