代码改变世界

MVC3+Entity Framework 实现投票系统(三)

2011-11-28 20:15  张剑  阅读(421)  评论(0编辑  收藏  举报
接上一节,我们通过控制器来添加视图页面:

1.着先在view目录中的Shared(共享)目录中添加新建项,MVC视图母版页:

2.添加完成后如下:

3.打开控制器目录中的HomeController类,对着Index方法点右建,添加视图,并选择“强类型”,添写内容为List<MvcApplication16.Models.Users>,选择母板页为刚刚添加的ViewwMasterPage.Master页面。生成如下代码:

  1. <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/ViewMasterPage.Master" Inherits="System.Web.Mvc.ViewPage<List<MvcApplication16.Models.Users>>" %>
  2. <asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
  3. Index
  4. </asp:Content>
  5. <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
  6. <script type="text/javascript">
  7. var i = 0;
  8. function MyVote(id) {
  9. $.get("Vote.ashx?i="+i, { id: id }, function (data) {
  10. if (data != "0") {
  11. $("#a" + id).html(data);
  12. alert("投票成功!");
  13. } else {
  14. alert("投票失败!");
  15. }
  16. i++;
  17. });
  18. }
  19. </script>
  20. <h2>Index</h2>
  21. <a href="/Admin/Index/">管理投票</a>
  22. <table bgcolor="#3333ff" cellpadding="1" cellspacing="1" width="95%" align="center" >
  23. <tr>
  24. <% foreach (var v in Model)
  25. { %>
  26. <td align="center" bgcolor="white">
  27. <img src="/Content/<%=v.UserPicPath %>" width="110" height="110" /><br />
  28. 姓名: <%=v.UserName %> 票数:<span id="a<%=v.id %>"><%=v.VoteCount %></span><br />
  29. <input type="button" id="tp" onclick="MyVote(<%=v.id %>)" value="投票" />
  30. </td>
  31. <%} %>
  32. </tr>
  33. </table>
  34. </asp:Content>

4.为AdminController控制器Index方法添加视图,并指定强类型MvcApplication16.Models.Users,生成代码如下:

  1. <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/ViewMasterPage.Master" Inherits="System.Web.Mvc.ViewPage<MvcApplication16.Models.Users>" %>
  2. <asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
  3. Index
  4. </asp:Content>
  5. <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
  6. <h2>添加要参与投票的用户:</h2>
  7. <% List<MvcApplication16.Models.Users> list = (List<MvcApplication16.Models.Users>)View.List; //获取list集合,并转换为List<Users>类型
  8. %>
  9. <table bgcolor="#3333ff" cellpadding="1" cellspacing="1" width="95%" align="center" >
  10. <tr><th>用户名</th><th>票数</th><th>头像</th><th>操作</th></tr>
  11. <%foreach (var v in list)
  12. {%>
  13. <tr><td bgcolor="white"><%=v.UserName %></td><td bgcolor="white"><%=v.VoteCount %></td><td bgcolor="white"><%=v.UserPicPath %></td><td bgcolor="white"><a href="/Admin/Delete/?id=<%=v.id %>">删除</a> <a href="">修改</a></td></tr>
  14. <% } %>
  15. </table>
  16. <%using (Html.BeginForm("Create", "Admin", FormMethod.Post, new { enctype = "multipart/form-data" }))
  17. {%>
  18. <table bgcolor="#3333ff" cellpadding="1" cellspacing="1" width="95%" align="center" >
  19. <tr height="40"><td bgcolor="white">用户名:</td><td bgcolor="white"><%=Html.TextBoxFor(m => m.UserName)%></td></tr>
  20. <tr height="40"><td bgcolor="white">头像:</td><td bgcolor="white"><input type="file" name="up" /> </td></tr>
  21. <tr height="40"><td bgcolor="white">票数:</td><td bgcolor="white"><%=Html.TextBoxFor(m => m.VoteCount)%></td></tr>
  22. <tr height="40"><td bgcolor="white">操作:</td><td bgcolor="white"><input type="submit" value="添加用户" /></td></tr>
  23. </table>
  24. <%} %>
  25. </asp:Content>

6.为AdminController控制器中Edit(GET)方法添加视图,并指定强类型MvcApplication16.Models.Users,代码如下:

  1. <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/ViewMasterPage.Master" Inherits="System.Web.Mvc.ViewPage<MvcApplication16.Models.Users>" %>
  2. <asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
  3. Edit
  4. </asp:Content>
  5. <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
  6. <h2>Edit</h2>
  7. <%using (Html.BeginForm("Edit", "Admin", FormMethod.Post, new { enctype = "multipart/form-data" }))
  8. {%>
  9. <table bgcolor="#3333ff" cellpadding="1" cellspacing="1" width="95%" align="center" >
  10. <tr height="40"><td bgcolor="white">用户名:</td><td bgcolor="white"><%=Html.TextBoxFor(m => m.UserName)%></td></tr>
  11. <tr height="40"><td bgcolor="white">头像:</td><td bgcolor="white"><input type="file" name="up" /> </td></tr>
  12. <tr height="40"><td bgcolor="white">票数:</td><td bgcolor="white"><%=Html.TextBoxFor(m => m.VoteCount)%></td></tr>
  13. <tr height="40"><td bgcolor="white">操作:</td><td bgcolor="white"><input type="submit" value="添加用户" /></td></tr>
  14. </table>
  15. <%} %>
  16. </asp:Content>

以上代码中,MvcApplication16,为项目的名称及命名空间,可以改为你的项目名或命名空间。

内容源码如下:(无数据库,请自建)

下载源码