MVC3+Entity Framework 实现投票系统(三)
2011-11-27 15:43
309 查看
接上一节,我们通过控制器来添加视图页面:
1.着先在view目录中的Shared(共享)目录中添加新建项,MVC视图母版页:
2.添加完成后如下:
3.打开控制器目录中的HomeController类,对着Index方法点右建,添加视图,并选择“强类型”,添写内容为List<MvcApplication16.Models.Users>,选择母板页为刚刚添加的ViewwMasterPage.Master页面。生成如下代码:
4.为AdminController控制器Index方法添加视图,并指定强类型MvcApplication16.Models.Users,生成代码如下:
6.为AdminController控制器中Edit(GET)方法添加视图,并指定强类型MvcApplication16.Models.Users,代码如下:
以上代码中,MvcApplication16,为项目的名称及命名空间,可以改为你的项目名或命名空间。
内容源码如下:(无数据库,请自建)
附件:http://down.51cto.com/data/2359367
1.着先在view目录中的Shared(共享)目录中添加新建项,MVC视图母版页:
2.添加完成后如下:
3.打开控制器目录中的HomeController类,对着Index方法点右建,添加视图,并选择“强类型”,添写内容为List<MvcApplication16.Models.Users>,选择母板页为刚刚添加的ViewwMasterPage.Master页面。生成如下代码:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/ViewMasterPage.Master" Inherits="System.Web.Mvc.ViewPage<List<MvcApplication16.Models.Users>>" %> <asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server"> Index </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> <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,生成代码如下:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/ViewMasterPage.Master" Inherits="System.Web.Mvc.ViewPage<MvcApplication16.Models.Users>" %> <asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server"> Index </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> <h2>添加要参与投票的用户:</h2> <% List<MvcApplication16.Models.Users> list = (List<MvcApplication16.Models.Users>)View.List; //获取list集合,并转换为List<Users>类型 %> <table bgcolor="#3333ff" cellpadding="1" cellspacing="1" width="95%" align="center" > <tr><th>用户名</th><th>票数</th><th>头像</th><th>操作</th></tr> <%foreach (var v in list) {%> <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> <% } %> </table> <%using (Html.BeginForm("Create", "Admin", FormMethod.Post, new { enctype = "multipart/form-data" })) {%> <table bgcolor="#3333ff" cellpadding="1" cellspacing="1" width="95%" align="center" > <tr height="40"><td bgcolor="white">用户名:</td><td bgcolor="white"><%=Html.TextBoxFor(m => m.UserName)%></td></tr> <tr height="40"><td bgcolor="white">头像:</td><td bgcolor="white"><input type="file" name="up" /> </td></tr> <tr height="40"><td bgcolor="white">票数:</td><td bgcolor="white"><%=Html.TextBoxFor(m => m.VoteCount)%></td></tr> <tr height="40"><td bgcolor="white">操作:</td><td bgcolor="white"><input type="submit" value="添加用户" /></td></tr> </table> <%} %> </asp:Content>
6.为AdminController控制器中Edit(GET)方法添加视图,并指定强类型MvcApplication16.Models.Users,代码如下:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/ViewMasterPage.Master" Inherits="System.Web.Mvc.ViewPage<MvcApplication16.Models.Users>" %> <asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server"> Edit </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> <h2>Edit</h2> <%using (Html.BeginForm("Edit", "Admin", FormMethod.Post, new { enctype = "multipart/form-data" })) {%> <table bgcolor="#3333ff" cellpadding="1" cellspacing="1" width="95%" align="center" > <tr height="40"><td bgcolor="white">用户名:</td><td bgcolor="white"><%=Html.TextBoxFor(m => m.UserName)%></td></tr> <tr height="40"><td bgcolor="white">头像:</td><td bgcolor="white"><input type="file" name="up" /> </td></tr> <tr height="40"><td bgcolor="white">票数:</td><td bgcolor="white"><%=Html.TextBoxFor(m => m.VoteCount)%></td></tr> <tr height="40"><td bgcolor="white">操作:</td><td bgcolor="white"><input type="submit" value="添加用户" /></td></tr> </table> <%} %> </asp:Content>
以上代码中,MvcApplication16,为项目的名称及命名空间,可以改为你的项目名或命名空间。
内容源码如下:(无数据库,请自建)
附件:http://down.51cto.com/data/2359367
相关文章推荐
- MVC3+Entity Framework 实现投票系统(一) 推荐
- MVC3+Entity Framework 实现投票系统(二)
- SpringMVC学习(12):基于Hibernate+Spring+Spring MVC+Bootstrap的管理系统实现
- php投票系统简单实现源码(1/3)
- spring boot+mvc+mybatis+netty-sokey.io+html+js实现简单即时通讯聊天系统
- 基于MVC网上报名系统的实现
- SpringMVC学习系列(11) 完结篇 之 基于Hibernate+Spring+Spring MVC+Bootstrap的管理系统实现
- (.net+MVC)导入资源功能的实现---权限系统
- sql server 关于表中只增标识问题 C# 实现自动化打开和关闭可执行文件(或 关闭停止与系统交互的可执行文件) ajaxfileupload插件上传图片功能,用MVC和aspx做后台各写了一个案例 将小写阿拉伯数字转换成大写的汉字, C# WinForm 中英文实现, 国际化实现的简单方法 ASP.NET Core 2 学习笔记(六)ASP.NET Core 2 学习笔记(三)
- 使用MVC实现用户登录注册系统—银行管理系统
- SpringMvc+Mybatis实现一个简单人事管理系统(三)
- MVC3+Entity Framework 实现投票系统(二)
- spring boot+mvc+mybatis(通用mapper)+druid+jsp+bootstrap实现后台权限管理系统源码
- SpringMVC学习系列(12) 完结篇 之 基于Hibernate+Spring+Spring MVC+Bootstrap的管理系统实现
- 校园视频管理系统----EF+WCF+MVC实现SOA架构
- JSP+MySql实现的一个简单投票系统
- JSP+MySql实现的一个简单投票系统
- “MVC+Nhibernate+Jquery-EasyUI” 信息发布系统 第四篇(用户管理功能的实现)
- ASP.NET MVC 3实现访问统计系统
- springmvc+mybatis实现简单的图书管理系统