MVC2脚手架-看不到任何数据
我在查看 MVC2 应用程序中的任何数据时遇到问题。
我正在使用典型的脚手架设置,但是当我查看索引或详细信息页面时,我根本看不到任何数据。
我确信我在这里遗漏了一些简单的东西,因为我在编译或运行时没有收到错误。
这是我的模型(Users.cs):
public class Users
{
public string UserName { get; set; }
[Required(ErrorMessage = "The Customer ID is required.")]
public string CustomerID { get; set; }
}
这是重要的 2 个控制器(UsersController.cs)操作:
public class UsersController : Controller
{
static List<Users> users = new List<Users>();
//
// GET: /Users/
public ActionResult Index()
{
return View(users);
}
//
// GET: /Users/Details/5
public ActionResult Details(Users u)
{
return View(u);
}
}
这是我的观点。
Users/Index.aspx
<table>
<% foreach (var item in Model) { %>
<tr>
<td>
<%= Html.ActionLink("Edit", "Edit", new { id=item.UserName }) %> |
<%= Html.ActionLink("Details", "Details", item )%> |
<%= Html.ActionLink("Delete", "Delete", new { id = item.UserName })%>
</td>
<td>
<%= Html.Encode(item.UserName) %>
</td>
<td>
<%= Html.Encode(item.CustomerID) %>
</td>
</tr>
<% } %>
</table>
<p>
<%= Html.ActionLink("Create New", "Create") %>
</p>
Users/Details.aspx
<fieldset>
<legend>Fields</legend>
<div class="display-label">UserName</div>
<div class="display-field"><%= Html.Encode(Model.UserName) %></div>
<div class="display-label">CustomerID</div>
<div class="display-field"><%= Html.Encode(Model.CustomerID) %></div>
</fieldset>
<p>
<%= Html.ActionLink("Edit", "Edit", new { id=Model.UserName }) %> |
<%= Html.ActionLink("Back to List", "Index") %>
</p>
所有页面加载都很好,没有错误,但没有显示任何数据。如果我运行调试器,用户列表有 0 个条目。
我正在使用 SQL Server。我的连接字符串以典型方式位于我的 web.config 中:
我的数据库中已经有用户数据。它是使用默认的 asp.net 网站管理工具生成的,我使用该工具进行身份验证。这非常有效。
那么我做错了什么?感谢任何帮助,如果我需要提供更多信息,请告诉我。
I'm having a problem seeing any data in my MVC2 application.
I'm using your typical scaffolding setup, but when I view the index or details page I see no data at all.
I'm sure I'm missing something simple here as I get no errors when compiling or running.
Here is my model(Users.cs):
public class Users
{
public string UserName { get; set; }
[Required(ErrorMessage = "The Customer ID is required.")]
public string CustomerID { get; set; }
}
Here are the 2 controller(UsersController.cs) actions that matter:
public class UsersController : Controller
{
static List<Users> users = new List<Users>();
//
// GET: /Users/
public ActionResult Index()
{
return View(users);
}
//
// GET: /Users/Details/5
public ActionResult Details(Users u)
{
return View(u);
}
}
And here are my views.
Users/Index.aspx
<table>
<% foreach (var item in Model) { %>
<tr>
<td>
<%= Html.ActionLink("Edit", "Edit", new { id=item.UserName }) %> |
<%= Html.ActionLink("Details", "Details", item )%> |
<%= Html.ActionLink("Delete", "Delete", new { id = item.UserName })%>
</td>
<td>
<%= Html.Encode(item.UserName) %>
</td>
<td>
<%= Html.Encode(item.CustomerID) %>
</td>
</tr>
<% } %>
</table>
<p>
<%= Html.ActionLink("Create New", "Create") %>
</p>
Users/Details.aspx
<fieldset>
<legend>Fields</legend>
<div class="display-label">UserName</div>
<div class="display-field"><%= Html.Encode(Model.UserName) %></div>
<div class="display-label">CustomerID</div>
<div class="display-field"><%= Html.Encode(Model.CustomerID) %></div>
</fieldset>
<p>
<%= Html.ActionLink("Edit", "Edit", new { id=Model.UserName }) %> |
<%= Html.ActionLink("Back to List", "Index") %>
</p>
All the pages load just fine with no errors, but no data is shown. If I run the debugger the users list has 0 entries.
I am using SQL Server. My connection string is in my web.config in the typical fashion:
My database has user data in it already. It was generated with the default asp.net Web Site Administration Tool, which I am using for authentication. This works perfectly.
So what am I doing wrong? Any help is appreciated and if I need to give more info please let me know.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的 UserController (如 OP 评论中所解释的)实例化您的用户列表,没有数据。
如果您想要播种数据,请考虑按如下方式更改声明:
关于您的“持久性”的快速建议(如果您愿意,请忽略此,只是一个友好的提示)...
考虑引入持久层。也就是说,如果你想持久化数据,可以考虑将其存储在数据库、xml文件等中。看看这里。这是 MVC 上的一个很好的资源,有很多示例和教程,在“模型”部分的中间,它向您介绍了实体框架,它可以很好地用于存储、检索和更新数据库中的数据。
Your UserController (as explained in comment on OP) instantiates your list of users with no data.
If you want to seed data, consider changing the declaration as follows:
A quick suggestion regarding your "persistance" (ignore this if you like, just a friendly tip)...
Consider introducting a persistance layer. That is, if you want to persist data, consider storing it in a database, xml file, etc. Take a look here. It's a great resource on MVC with lots of samples and tutorials, and half way down under the "Models" section it introduces you to the Entity Framework which can be used to store, retrieve, and update your data on a database quite nicely.