如何在视图中显示数据 [英] How can i display data in a View
本文介绍了如何在视图中显示数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在asp.net核心MVC中的视图中显示数据?在Index.cshtml中,我具有指向详细信息页面的以下链接.
How can i display data in views in asp.net core MVC?. In the Index.cshtml I have the following link to the detail page.
@Html.ActionLink("You_Controller_Name", "GetProductsDetail", new { id = item.ID }) |
我有这个控制器来通过ID获取产品
I have this controller to get product by ID
public IActionResult Detail()
{
return View();
}
[HttpGet()]
public async Task<IActionResult> GetProductsDetail(string id)
{
var product_list = (await ProductService.GetProducts()).ToList();
var product = product_list.FirstOrDefault(a => a.ProductCode == id);
return view(product);
}
需要在详细信息页面上显示产品信息的帮助.
Need help on displaying Product information in the detail page.
推荐答案
您应该在GetProductsDetail
操作
return View("Detail", product);
阅读以下内容以更好地理解
Read the following to have a better understanding
已更新
您可以这样存储
@ViewBag.ProductName = product.ProductName
在视图中:
<h1>@ViewBag.ProductName</h1>
完整代码
[HttpGet]
public async Task<IActionResult> GetProductsDetail(string id)
{
var product_list = (await ProductService.GetProducts()).ToList();
var product = product_list.FirstOrDefault(a => a.ProductCode == id);
@ViewBag.ProductName = product.ProductName
return View("Detail", product); // Make sure that in View is expecting `ProductList`, Otherwise, You just return View("Detail");
}
这篇关于如何在视图中显示数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文