如何在MVC的cshtml视图中显示项目列表? [英] How do I display a list of items in my cshtml view in MVC ?
问题描述
我参与了一个电子商务网站的asp.net网络表单项目,其中我展示了一个项目列表,例如使用REPEATER的电子产品。
现在我正在开发一个mvc项目,但我不知道如何显示这些产品。
< blockquote> 使用ASP.NET MVC查看通用列表。一步一步预览5 - PeterKellner.net [ ^ ]
其他示例的Googleasp.net mvc show list。我建议你买一本关于MVC的书,并通过它来学习基础知识,因为你无法从论坛帖子中学习新技术。如果你坚持以这种方式学习,那么首先谷歌你的问题,因为有很多教程。
一个基本的例子是
控制器行动
public ActionResult Index()
{
var data = // 获取数据的一些方法
return 查看(数据);
}
查看 - Index.cshtml
@ model IEnumerable < your_data_model_or_entity_class_here >
< table >
< thead > ;
< tr >
< th > 您的列标题名称< / th >
< / tr >
< / thead >
< tbody >
@foreach(模型中的var项目)
{
< span class =code-keyword>< tr >
< td >
<! - 在此处执行操作以表格格式显示项目(或者您想要的任何格式,您想要的列数 - >
< ; / td >
< / tr >
}
< / tbody >
< 表格 >
< / table > < span class =code-keyword>< / table > < / your_data_model_or_entity_class_here >
但是像sitecore所说的那样,学习MVC最好是遵循以下教程比这篇帖子更能更彻底地解释这样的问题。
I have worked on a asp.net web form project for an ecommerce website where i have displayed a list of items say electronic products using a REPEATER.
Now i am working on an mvc project but i have no idea how to display these products .
View a Generic List with ASP.NET MVC. Step by Step With Preview 5 - PeterKellner.net[^]
Google "asp.net mvc show list" for other examples. I'd recommend you get a book on MVC and go through it to learn the basics as you can't learn a new technology from forum posts. If you insist on learning this way then google your questions first as there are numerous tutorials out there.
A basic example would be
Controller Action
public ActionResult Index() { var data = //some method to get your data to display return View(data); }
View - Index.cshtml
@model IEnumerable<your_data_model_or_entity_class_here> <table> <thead> <tr> <th>Your Column Header Name</th> </tr> </thead> <tbody> @foreach(var item in Model) { <tr> <td> <!-- Do something here to display your items in table format (or whatever format you want with as many columns you want --> </td> </tr> } </tbody> <table> </table></table></your_data_model_or_entity_class_here>
But like sitecore said, learning MVC would be best by following tutorials that better explain issues like this more thoroughly than this post does.
这篇关于如何在MVC的cshtml视图中显示项目列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!