欢迎来到飞鸟慕鱼博客,开始您的技术之旅!
当前位置: 首页知识笔记正文

Title: MVC3 Entity Framework to Realize Voting System (3)

终极管理员 知识笔记 26阅读

Continued from the previous section, we add view pages through the controller: 1. First, add new items in the shared (shared) directory in the perspective directory, and the MVC view master page: 2. After the addition is completed, it is as follows: 3. Open the family controller class in the controller directory, and click on the right to build the index method. Master the page. Generate the following code:% @ pagetitle='' language=' c #' masterpage=' ~/views/shared/viewmasterpage. Main "inheritors=" system. web . MVC . viewpagelistmvcaplication 16 . models . users ' % ASP : content ID=' content 1 ' ContentPlaceHolderID=' title content ' runat=' s erver ' Index/ASP : content ASP 3360 content ID=' content 2 ' ContentPlaceHolderID=' main content ' runat=' server '

/li>
  • <script type="text/javascript">
  • var i = 0;
  • function MyVote(id) {
  • $.get("Vote.ashx?i="+i, { id: id }, function (data) {
  • if (data != "0") {
  • $("#a" + id).html(data);
  • alert("投票成功!");
  • } else {
  • alert("投票失败!");
  • }
  • i++;
  • });
  • }
  • </script>
  • <h2>Index</h2>
  • <a href="/Admin/Index/">管理投票</a>
  • <table bgcolor="#3333ff" cellpadding="1" cellspacing="1" width="95%" align="center" >
  • <tr>
  • <% foreach (var v in Model)
  • { %>
  • <td align="center" bgcolor="white">
  • <img src="/Content/<%=v.UserPicPath %>" width="110" height="110" /><br />
  • 姓名: <%=v.UserName %> 票数:<span id="a<%=v.id %>"><%=v.VoteCount %></span><br />
  • <input type="button" id="tp" onclick="MyVote(<%=v.id %>)" value="投票" />
  • </td>
  • <%} %>
  • </tr>
  • </table>
  • </asp:Content>
  • 4.为AdminController控制器Index方法添加视图,并指定强类型MvcApplication16.Models.Users,生成代码如下:

    1. <%@ Page Title="" Language="C#" MasterPage>="~/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#" MasterPage>="~/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,为项目的名称及命名空间,可以改为你的项目名或命名空间。

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

    下载源码

    标签:
    声明:无特别说明,转载请标明本文来源!