【问题标题】:How can i display Sales made by customer in customers detail page如何在客户详细信息页面中显示客户的销售额
【发布时间】:2020-02-15 02:57:53
【问题描述】:

如何显示来自多个模型的数据。我有客户和销售表,当示意查看细节时,我想查看所选客户的销售。

客户模型

namespace MasterDetailMVC.Models
{
    using System;
    using System.Collections.Generic;

    public partial class Customer
    {
        public Customer()
        {
            this.Sales = new HashSet<Sale>();
        }

        public int CustomerID { get; set; }
        public string FirstName { get; set; }
        public string MiddleInitial { get; set; }
        public string LastName { get; set; }


       public virtual ICollection<Sale> Sales { get; set; }
    }
  }

销售模式

namespace MasterDetailMVC.Models
{
    using System;
    using System.Collections.Generic;

    public partial class Sale
    {
        public int SalesID { get; set; }
        public int SalesPersonID { get; set; }
        public int CustomerID { get; set; }
        public int ProductID { get; set; }
        public int Quantity { get; set; }

        public virtual Customer Customer { get; set; }
        public virtual Employee Employee { get; set; }
        public virtual Product Product { get; set; }
    }
}

客户控制器

namespace MasterDetailMVC.Controllers
{
public class CustomersController : Controller
{
private salesdbEntities db = new salesdbEntities();
// GET: Customers
public ActionResult Index()
{
 return View(db.Customers.ToList());
 }
 // GET: Customers/Details/5
 public ActionResult Details(int? id)
 {
 Customer customer = db.Customers.Find(id);
  if (customer == null)
  {
  return HttpNotFound();
 }
  return View(customer);
 }
 } 

索引视图

@model IEnumerable<MasterDetailMVC.Models.Customer>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.FirstName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.MiddleInitial)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.LastName)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.FirstName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.MiddleInitial)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.LastName)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.CustomerID }) |
            @Html.ActionLink("Details", "Details", new { id=item.CustomerID }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.CustomerID })
        </td>
    </tr>
}

</table>

详细视图

@model MasterDetailMVC.Models.Customer

@{
    ViewBag.Title = "Details";
}

<h2>Details</h2>

<div>
    <h4>Customer</h4>
    <hr />
    <dl class="dl-horizontal">
        <dt>
            @Html.DisplayNameFor(model => model.FirstName)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.FirstName)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.MiddleInitial)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.MiddleInitial)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.LastName)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.LastName)
        </dd>

    </dl>
</div>
<p>
    @Html.ActionLink("Edit", "Edit", new { id = Model.CustomerID }) |
    @Html.ActionLink("Back to List", "Index")
</p>

当我转到此链接时Customers/Details/5 我想在表格或网格中显示该客户疯狂的销售。

销售表是这样的

SELECT TOP (1000) [SalesID]
      ,[SalesPersonID]
      ,[CustomerID]
      ,[ProductID]
      ,[Quantity]
  FROM [salesdb].[dbo].[Sales]

产品表是这样的

SELECT TOP (1000) [ProductID]
      ,[Name]
      ,[Price]
  FROM [salesdb].[dbo].[Products]

销售人员/员工表

 SELECT TOP (1000) [EmployeeID]
      ,[FirstName]
      ,[MiddleInitial]
      ,[LastName]
  FROM [salesdb].[dbo].[Employees]

【问题讨论】:

  • 视图是什么样的?
  • 添加了客户视图

标签: asp.net


【解决方案1】:

型号

你需要创建CustomerViewModel

public class CustomerViewModel
{
    public int CustomerID { get; set; }
    public string FirstName { get; set; }
    public string MiddleInitial { get; set; }
    public string LastName { get; set; }

    public List<SaleViewModel> Sales { get; set; }
}

SaleViewModel 如下所示

public class SaleViewModel
{
    public int ProductId { get; set; }
    public string ProductName { get; set; }
    public decimal Price { get; set; }
    public int Quantity { get; set; }
}

控制器

您可以通过加入SalesProducts 表格来获得视图列表SaleViewModel,如下所示

var sales = (from s in db.Sales
                join p in db.Products on s.ProductId equals p.ProductID
                where s.CustomerID == id
                select new SaleViewModel 
                {
                    ProductId = s.ProductId,
                    ProductName = p.ProductName,
                    Price = p.Price
                    Quantity = s.Quantity
                }).ToList();
var result = new CustomerViewModel 
{
  CustomerID = customer.CustomerID, 
  FirstName = customer.FirstName,
  MiddleInitial = customer.MiddleInitial,
  LastName = customer.LastName,
  Sales = sales
}

查看

@model MasterDetailMVC.Models.CustomerViewModel

@{
     ViewBag.Title = "List of item by Customer";
}
<table>
    <th>
      <td>Product Name </td>
      // The rest of items....
    </th>
    @foreach(var item in Model.Sales)
        <tr>
            <td>@item.ProductName</td>
            // The rest of items....
        </tr>
    }
</table>

【讨论】:

  • 是新的控制器还是更新的细节控制器
  • 等等,详细视图中正在等待@model MasterDetailMVC.Models.Customer
  • 在这一行` where s.CustomerID = id` 我得到`can not convert int to bool"
  • 应该是s.CustomerID == id。无论如何,我刚刚更新了我的答案,请看一下
  • 好的,你能检查一下这个 Phong 吗? stackoverflow.com/questions/60348462/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-26
  • 1970-01-01
  • 2018-10-19
  • 1970-01-01
  • 2019-06-18
  • 1970-01-01
相关资源
最近更新 更多