【问题标题】:Display data from a table in a database to an ASP.net mvc 4 Web application将数据库中的表中的数据显示到 ASP.net mvc 4 Web 应用程序
【发布时间】:2013-07-23 20:33:01
【问题描述】:
我是 ASP.Net MVC 4 的新手。
我需要在网格或 webgrid 或 web 应用程序页面中的什么中显示列及其数据。我应该通过 Asp.net MVC 4, Visual Studio 2012 来实现应用程序。
有任何帮助或资源来指导我吗?
环境。
SQL 服务器 2012
视觉工作室 2012
【问题讨论】:
标签:
asp.net-mvc-4
visual-studio-2012
gridview
webgrid
【解决方案1】:
考虑到您有一个基于该数据库的数据模型(使用实体框架),您可以通过在您的视图上使用常规 HTML 表来实现这一点(我们称之为 Table.cshtml):
@model IEnumerable<YourModelClass>
<!-- Using Regular HTML Tables -->
<table>
<tr>
<th>
@Html.DisplayNameFor(model => model.FirstOrDefault().Property1)
</th>
<th>
@Html.DisplayNameFor(model => model.FirstOrDefault().Property2)
</th>
(...)
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Property1)
</td>
<td>
@Html.DisplayFor(modelItem => item.Property2)
</td>
(...)
</tr>
}
在 YourModel 控制器中,您有类似的内容:
public ActionResult Table()
{
return View("Table", db.YourModel.ToList());
}
这很简单,通常由脚手架生成。我不知道这是否符合您的目的。
这是一个很好的初学者教程:http://www.asp.net/mvc/tutorials/mvc-music-store
除此之外,网络上还有很多优秀的“网格”组件,它们具有各种“奇特的功能”,例如排序/过滤/等。 (例如:我使用过 DevExpress)。