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. <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,为项目的名称及命名空间,可以改为你的项目名或命名空间。

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

下载源码

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值