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

 接上一节,我们通过控制器来添加视图页面:

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.  
  3. <asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server"> 
  4.     Index 
  5. </asp:Content> 
  6.  
  7. <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> 
  8. <script type="text/javascript"> 
  9.     var i = 0
  10.     function MyVote(id) { 
  11.         $.get("Vote.ashx?i="+i, { id: id }, function (data) { 
  12.             if (data != "0") { 
  13.                 $("#a" + id).html(data); 
  14.                 alert("投票成功!"); 
  15.             } else { 
  16.                 alert("投票失败!"); 
  17.             } 
  18.             i++; 
  19.         }); 
  20.     } 
  21. </script> 
  22.  
  23. <h2>Index</h2> 
  24. <a href="/Admin/Index/">管理投票</a> 
  25.  
  26. <table bgcolor="#3333ff" cellpadding="1" cellspacing="1" width="95%" align="center" > 
  27.     <tr> 
  28.     <% foreach (var v in Model) 
  29.        { %> 
  30.     <td align="center" bgcolor="white"> 
  31.     <img src="/Content/<%=v.UserPicPath %>" width="110" height="110" /><br /> 
  32.    姓名: <%=v.UserName %> 票数:<span id="a<%=v.id %>"><%=v.VoteCount %></span><br /> 
  33.    <input type="button" id="tp" onclick="MyVote(<%=v.id %>)" value="投票" /> 
  34.     </td> 
  35.     <%} %> 
  36.     </tr> 
  37.  
  38. </table> 
  39. </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.  
  3. <asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server"> 
  4.     Index 
  5. </asp:Content> 
  6.  
  7. <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> 
  8.     <h2>添加要参与投票的用户:</h2> 
  9. <% List<MvcApplication16.Models.Users> list = (List<MvcApplication16.Models.Users>)View.List; //获取list集合,并转换为List<Users>类型 
  10.     %> 
  11. <table bgcolor="#3333ff" cellpadding="1" cellspacing="1" width="95%" align="center" > 
  12. <tr><th>用户名</th><th>票数</th><th>头像</th><th>操作</th></tr> 
  13. <%foreach (var v in list) 
  14.   {%> 
  15. <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> 
  16. <% } %> 
  17. </table> 
  18.  
  19. <%using (Html.BeginForm("Create", "Admin", FormMethod.Post, new { enctype = "multipart/form-data" })) 
  20.   {%> 
  21. <table bgcolor="#3333ff" cellpadding="1" cellspacing="1" width="95%" align="center" > 
  22.     <tr height="40"><td bgcolor="white">用户名:</td><td bgcolor="white"><%=Html.TextBoxFor(m => m.UserName)%></td></tr> 
  23.     <tr height="40"><td bgcolor="white">头像:</td><td bgcolor="white"><input type="file" name="up" /> </td></tr> 
  24.     <tr height="40"><td bgcolor="white">票数:</td><td bgcolor="white"><%=Html.TextBoxFor(m => m.VoteCount)%></td></tr> 
  25.      <tr height="40"><td bgcolor="white">操作:</td><td bgcolor="white"><input type="submit" value="添加用户" /></td></tr> 
  26. </table> 
  27.  
  28. <%} %> 
  29.  
  30. </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.  
  3. <asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server"> 
  4.     Edit 
  5. </asp:Content> 
  6.  
  7. <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> 
  8.  
  9. <h2>Edit</h2> 
  10. <%using (Html.BeginForm("Edit", "Admin", FormMethod.Post, new { enctype = "multipart/form-data" })) 
  11.   {%> 
  12. <table bgcolor="#3333ff" cellpadding="1" cellspacing="1" width="95%" align="center" > 
  13.     <tr height="40"><td bgcolor="white">用户名:</td><td bgcolor="white"><%=Html.TextBoxFor(m => m.UserName)%></td></tr> 
  14.     <tr height="40"><td bgcolor="white">头像:</td><td bgcolor="white"><input type="file" name="up" /> </td></tr> 
  15.     <tr height="40"><td bgcolor="white">票数:</td><td bgcolor="white"><%=Html.TextBoxFor(m => m.VoteCount)%></td></tr> 
  16.      <tr height="40"><td bgcolor="white">操作:</td><td bgcolor="white"><input type="submit" value="添加用户" /></td></tr> 
  17. </table> 
  18.  
  19. <%} %> 
  20. </asp:Content> 

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值