【发布时间】: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