我正在处理一个 ASP.NET Core 8 MVC 项目,其中有两个简单的表,分别填充产品和客户端。当单击查看或编辑实体时(此时哪个实体并不重要),它应该使用产品或客户信息填充页面,但是,我得到一个空引用。
我已经调试了这个问题,看起来当我调用视图页面详细信息时,传递的 ID 是 0。如果它可以获取产品和/或客户并为两个表显示它们,为什么会出现这种情况?我还通过
asp-route-id
传递 id。
Dashboard.cshtml
:
<div class="row">
<h2>Products</h2>
<table class="table table-striped">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Product Name</th>
<th scope="col">Part Number</th>
<th scope="col">UPC</th>
<th scope="col">Price</th>
<th scope="col">Actions</th>
</tr>
</thead>
<tbody>
@foreach(var product in @ViewBag.Products)
{
<tr>
<td>@product.Id</td>
<td>@product.ProductName</td>
<td>@product.PartNumber</td>
<td>@product.UPC</td>
<td>[email protected]</td>
<td>
<a class="btn btn-primary" asp-area="" asp-controller="Admin" asp-action="ViewProductDetails" asp-route-id="@product.Id">View</a>
<a class="btn btn-primary" asp-area="" asp-controller="Admin" asp-action="UpdateProduct" asp-route-id="@product.Id">Update</a>
</td>
</tr>
}
</tbody>
</table>
</div>
<div class="row">
<h2>Clients</h2>
<table class="table table-striped">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Address</th>
<th scope="col">Phone Number</th>
<th scope="col">Actions</th>
</tr>
</thead>
<tbody>
@foreach(var client in @ViewBag.Clients)
{
<tr>
<td>@client.Id</td>
<td>@client.Address</td>
<td>@client.PhoneNumber</td>
<td>
<a class="btn btn-primary" asp-area="" asp-controller="Admin" asp-action="ViewClientDetails" asp-route-id="@client.Id">View</a>
<a class="btn btn-primary" asp-area="" asp-controller="Admin" asp-action="UpdateClient" asp-route-id="@client.Id">Update</a>
</td>
</tr>
}
</tbody>
</table>
</div>
AdminController
:
public async Task<IActionResult> Dashboard()
{
List<Client> clients = await _dbContext.Clients.OrderBy(client => client.Id).ToListAsync();
List<Product> products = await _dbContext.Products.OrderBy(products => products.Id).ToListAsync();
// Gets both Lists just fine and displays them
ViewBag.Clients = clients;
ViewBag.Products = products;
return View();
}
// Other code
// These methods are doing the same behavior. int of either productId or clientId are 0
// It shouldn't be 0
public async Task<IActionResult> UpdateProduct(int productId)
{
Product prod = await _dbContext.Products.FirstOrDefaultAsync(product => product.Id == productId);
return View(prod);
}
public async Task<IActionResult> ViewProductDetails(int productId)
{
Product prod = await _dbContext.Products.FirstOrDefaultAsync(product => product.Id == productId);
return View(prod);
}
public async Task<IActionResult> UpdateClient(int clientId)
{
Client client = await _dbContext.Clients.FirstOrDefaultAsync(client => client.Id == clientId);
return View(client);
}
public async Task<IActionResult> ViewClientDetails(int clientId)
{
Client client = await _dbContext.Clients.FindAsync(clientId);
return View(client);
}
当我调用视图页面详细信息时,传递的 ID 是 0。如果它可以获取产品和/或客户并在两个表中显示它们,为什么会这样?
您可以通过 使用
asp-route-{value}
属性通过路由数据传递值,在标签助手中使用 asp-route-id="@product.Id"
,请确保您的操作参数名称与该路由参数名称一致。
要解决问题,您可以修改操作参数,如下所示。
public async Task<IActionResult> ViewProductDetails(int id)
{
//...