在 PartialView 中仅查看数据库中包含 'Foo' 的行
View only rows containing 'Foo' from Database in PartialView
我有两个数据库。一个数据库包含产品及其详细信息。每个产品都有一个特定的库存编号(例如 AG1000)。第二个数据库包含有关员工输入的每个产品的评论(例如,更新日期、评论以及包含产品库存编号的列)。
对于底部的产品详细信息页面,我想填充一个 table 显示第二个数据库中的所有条目,其中包含显示的产品详细信息的库存编号。
我已成功将分部视图和占位符传递给视图 table。但不幸的是,我这辈子都无法将库存编号传递给部分并让 controller/partial 将数据库 2 中的所有行显示到 table.
查看:
@model IdentitySample.Models.InventoryViewModels
@{
ViewBag.Title = "Details";
string StockNumber = Model.StockNumber;
}
<p class="text-right align-top">@Html.ActionLink("Back to List", "Index")</p>
<div class="container">
<hr />
<div class="card">
<div class="card-header"><h2 class="text-center">@Html.DisplayFor(model => model.Year) @Html.DisplayFor(model => model.Make) @Html.DisplayFor(model => model.Model) @Html.DisplayFor(model => model.Trim)</h2></div>
<div class="card-body">
<div class="row">
<div class="col"><img style="width:100%;" src="@Html.DisplayFor(model => model.PhotoUrl)" /></div>
<div class="col">
<div class="card">
<div class="card-header"> <h5>Vehicle Details</h5></div>
<div class="card-body">
<table class="table table-light table-hover table-striped thead-dark">
<tbody>
<tr>
<th>@Html.DisplayNameFor(model => model.StockNumber)</th>
<td>@Html.DisplayFor(model => model.StockNumber)</td>
</tr>
<tr>
<th>@Html.DisplayNameFor(model => model.Year)</th>
<td>@Html.DisplayFor(model => model.Year)</td>
</tr>
<tr>
<th>@Html.DisplayNameFor(model => model.Make)</th>
<td>@Html.DisplayFor(model => model.Make)</td>
</tr>
<tr>
<th>@Html.DisplayNameFor(model => model.Model)</th>
<td>@Html.DisplayFor(model => model.Model)</td>
</tr>
<tr>
<th>@Html.DisplayNameFor(model => model.Trim)</th>
<td>@Html.DisplayFor(model => model.Trim)</td>
</tr>
<tr>
<th>@Html.DisplayNameFor(model => model.Odometer)</th>
<td>@Html.DisplayFor(model => model.Odometer)</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
<br />
<div class="row">
<div class="col-12">
<div class="card" style="width: 100%;">
<div class="card-header align-middle"><h5>Vehicle Log<a href="#" class="fa fa-2x fa-plus-square float-right"></a></h5></div>
<div class="card-body">
@Html.Partial("_ViewActions", new List<IdentitySample.Models.InventoryActionsModels>())
</div>
</div>
</div>
</div>
</div>
</div>
</div>
_部分:
@model IEnumerable<IdentitySample.Models.InventoryActionsModels>
<h5>Test Inventory Action View: @ViewBag.StockNumber</h5>
<table class="table table-light table-hover table-striped thead-dark">
<tr>
<th>
@Html.DisplayNameFor(model => model.StockNumber)
</th>
<th>
@Html.DisplayNameFor(model => model.TouchType)
</th>
<th>
@Html.DisplayNameFor(model => model.TouchedBy)
</th>
<th>
@Html.DisplayNameFor(model => model.TouchDate)
</th>
<th>
@Html.DisplayNameFor(model => model.TouchComments)
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.StockNumber)
</td>
<td>
@Html.DisplayFor(modelItem => item.TouchType)
</td>
<td>
@Html.DisplayFor(modelItem => item.TouchedBy)
</td>
<td>
@Html.DisplayFor(modelItem => item.TouchDate)
</td>
<td>
@Html.DisplayFor(modelItem => item.TouchComments)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.Id }) |
@Html.ActionLink("Details", "Details", new { id = item.Id }) |
@Html.ActionLink("Delete", "Delete", new { id = item.Id })
</td>
</tr>
}
</table>
控制器:
// GET: InventoryActions
public ActionResult Index()
{
// ViewBag.StockNumber();
string vehicleId = StockNumber;
var vehicle = db.InventoryActionsModels.GroupBy(i => i.StockNumber)
.Select(g => new InventoryActionsModels()
{
StockNumber = vehicleId,
Id = g.Max(row => row.Id)
}).ToList();
return View(db.InventoryActionsModels.ToListAsync());
}
我猜你应该为库存详细信息创建一个复合视图模型,然后将它传递给你的视图,有点
public class InventoryDetailsViewModel
{
public InventoryModel Inventory { get; set; }
public ICollection<InventoryActionsModels> Actions { get; set; }
}
控制器
public async Task<ActionResult> GetInventoryDetails(string stockNumber)
{
var viewModel = new InventoryDetailsViewModel
{
Inventory = await GetInventoryById(stockNumber),
Actions = await GetInventoryActions(stockNumber)
};
return View(viewModel);
}
其中 GetInventoryById 和 GetInventoryActions 是从数据库获取信息的方法
然后在视图中
@model IdentitySample.Models.InventoryDetailsViewModel
@Html.Partial("_ViewInventory", Model.Inventory)
@Html.Partial("_ViewActions", Model.Actions)
我有两个数据库。一个数据库包含产品及其详细信息。每个产品都有一个特定的库存编号(例如 AG1000)。第二个数据库包含有关员工输入的每个产品的评论(例如,更新日期、评论以及包含产品库存编号的列)。
对于底部的产品详细信息页面,我想填充一个 table 显示第二个数据库中的所有条目,其中包含显示的产品详细信息的库存编号。
我已成功将分部视图和占位符传递给视图 table。但不幸的是,我这辈子都无法将库存编号传递给部分并让 controller/partial 将数据库 2 中的所有行显示到 table.
查看:
@model IdentitySample.Models.InventoryViewModels
@{
ViewBag.Title = "Details";
string StockNumber = Model.StockNumber;
}
<p class="text-right align-top">@Html.ActionLink("Back to List", "Index")</p>
<div class="container">
<hr />
<div class="card">
<div class="card-header"><h2 class="text-center">@Html.DisplayFor(model => model.Year) @Html.DisplayFor(model => model.Make) @Html.DisplayFor(model => model.Model) @Html.DisplayFor(model => model.Trim)</h2></div>
<div class="card-body">
<div class="row">
<div class="col"><img style="width:100%;" src="@Html.DisplayFor(model => model.PhotoUrl)" /></div>
<div class="col">
<div class="card">
<div class="card-header"> <h5>Vehicle Details</h5></div>
<div class="card-body">
<table class="table table-light table-hover table-striped thead-dark">
<tbody>
<tr>
<th>@Html.DisplayNameFor(model => model.StockNumber)</th>
<td>@Html.DisplayFor(model => model.StockNumber)</td>
</tr>
<tr>
<th>@Html.DisplayNameFor(model => model.Year)</th>
<td>@Html.DisplayFor(model => model.Year)</td>
</tr>
<tr>
<th>@Html.DisplayNameFor(model => model.Make)</th>
<td>@Html.DisplayFor(model => model.Make)</td>
</tr>
<tr>
<th>@Html.DisplayNameFor(model => model.Model)</th>
<td>@Html.DisplayFor(model => model.Model)</td>
</tr>
<tr>
<th>@Html.DisplayNameFor(model => model.Trim)</th>
<td>@Html.DisplayFor(model => model.Trim)</td>
</tr>
<tr>
<th>@Html.DisplayNameFor(model => model.Odometer)</th>
<td>@Html.DisplayFor(model => model.Odometer)</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
<br />
<div class="row">
<div class="col-12">
<div class="card" style="width: 100%;">
<div class="card-header align-middle"><h5>Vehicle Log<a href="#" class="fa fa-2x fa-plus-square float-right"></a></h5></div>
<div class="card-body">
@Html.Partial("_ViewActions", new List<IdentitySample.Models.InventoryActionsModels>())
</div>
</div>
</div>
</div>
</div>
</div>
</div>
_部分:
@model IEnumerable<IdentitySample.Models.InventoryActionsModels>
<h5>Test Inventory Action View: @ViewBag.StockNumber</h5>
<table class="table table-light table-hover table-striped thead-dark">
<tr>
<th>
@Html.DisplayNameFor(model => model.StockNumber)
</th>
<th>
@Html.DisplayNameFor(model => model.TouchType)
</th>
<th>
@Html.DisplayNameFor(model => model.TouchedBy)
</th>
<th>
@Html.DisplayNameFor(model => model.TouchDate)
</th>
<th>
@Html.DisplayNameFor(model => model.TouchComments)
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.StockNumber)
</td>
<td>
@Html.DisplayFor(modelItem => item.TouchType)
</td>
<td>
@Html.DisplayFor(modelItem => item.TouchedBy)
</td>
<td>
@Html.DisplayFor(modelItem => item.TouchDate)
</td>
<td>
@Html.DisplayFor(modelItem => item.TouchComments)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.Id }) |
@Html.ActionLink("Details", "Details", new { id = item.Id }) |
@Html.ActionLink("Delete", "Delete", new { id = item.Id })
</td>
</tr>
}
</table>
控制器:
// GET: InventoryActions
public ActionResult Index()
{
// ViewBag.StockNumber();
string vehicleId = StockNumber;
var vehicle = db.InventoryActionsModels.GroupBy(i => i.StockNumber)
.Select(g => new InventoryActionsModels()
{
StockNumber = vehicleId,
Id = g.Max(row => row.Id)
}).ToList();
return View(db.InventoryActionsModels.ToListAsync());
}
我猜你应该为库存详细信息创建一个复合视图模型,然后将它传递给你的视图,有点
public class InventoryDetailsViewModel
{
public InventoryModel Inventory { get; set; }
public ICollection<InventoryActionsModels> Actions { get; set; }
}
控制器
public async Task<ActionResult> GetInventoryDetails(string stockNumber)
{
var viewModel = new InventoryDetailsViewModel
{
Inventory = await GetInventoryById(stockNumber),
Actions = await GetInventoryActions(stockNumber)
};
return View(viewModel);
}
其中 GetInventoryById 和 GetInventoryActions 是从数据库获取信息的方法
然后在视图中
@model IdentitySample.Models.InventoryDetailsViewModel
@Html.Partial("_ViewInventory", Model.Inventory)
@Html.Partial("_ViewActions", Model.Actions)