[转载]asp.net mvc 2 简简单单做开发 通用增删改基本操作通用页面 – 飞创cms – 博客园.
ASP.NET mvc 2能使我们重复的工作中解脱出来,增删改等基本数据操作,都可以简单的实现。ASP.NET mvc 2 也借鉴不少ASP.NET Dynamic Data里的功能,通过实体类,只需要简单的操作即可以生成网站。我个人感觉,看似简单不过真正的用到开发中会遇到很多问题。自定义性太差。而 ASP.NET MVC 2里提供了很好的方式,页面的实现可以通过,自定义字段、自定义页面来实现。mvc 2 view的查找方式 “~/Views/{1}/{0}.aspx”, “~/Views/{1}{0}.ascx”,“~/Views/Shared/{0}.aspx”, “~/Views/Shared/{0}.ascx” ,把通用的页面放到”~/Views/Shared 文件下 。为了通用实体类采 dynamic来自动实现。 下面是 Index的代码,默认支持自定义查询,数据分页等基本功能。
1 <%@ Page Title=“” Language=“C#“ MasterPageFile=“~/Views/Shared/Admin.Master“ Inherits=“System.Web.Mvc.ViewPage<dynamic>“ %>
2 <%@ Import Namespace=“mvc.Models“ %>
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 <div id=“headImg“>
9 <img src=“/content/adminimages/01.gif“ alt=“” /><%:ViewData[“NavTitle“]%>
10 </div>
11 <div id=“cont2“>
12 <div class=“form clearfix“>
13
14 <form id=“form1“ method=“get“>
15 <div id=“Search“ class=“clearfix“>
16 <%=Html.DisplayFor(m => ViewData[“searchModel“], “tool“,“”)%>
17
18 <li class=“add“>
19 <%: Html.ActionLink(“添加“, “Add“) %>
20 </li>
21 </div>
22 </form>
23 <table class=“tb“>
24 <% int i = 0; foreach (var art in Model)
25 {
26 i++;
27 if (i == 1)
28 {
29 %>
30 <tr class=“tbhead“>
31 <%=Html.DisplayFor(m => art, “header“, “di“)%>
32
33 <th>操作</th>
34 </tr>
35 <%} %>
36 <tr>
37 <%=Html.DisplayFor(m => art, “rows“, “di“)%>
38 <td style=“width:60px“>
39 <%: Html.ActionLink(“编辑“, “Edit“, new { id = art.Id })%> <%: Html.ActionLink(“删除“, “Delete“, new { id = art.Id }, new { onclick = “return confirm(‘你确定要删除 吗?’)“ })%>
40 </td>
41 </tr>
42 <%} %>
43 </table>
44
45 <div class=“page“>
46 <%=ViewData[“page“] %>
47 </div>
48 </div>
49 </div>
50 </asp:Content>
51
2 <%@ Import Namespace=“mvc.Models“ %>
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 <div id=“headImg“>
9 <img src=“/content/adminimages/01.gif“ alt=“” /><%:ViewData[“NavTitle“]%>
10 </div>
11 <div id=“cont2“>
12 <div class=“form clearfix“>
13
14 <form id=“form1“ method=“get“>
15 <div id=“Search“ class=“clearfix“>
16 <%=Html.DisplayFor(m => ViewData[“searchModel“], “tool“,“”)%>
17
18 <li class=“add“>
19 <%: Html.ActionLink(“添加“, “Add“) %>
20 </li>
21 </div>
22 </form>
23 <table class=“tb“>
24 <% int i = 0; foreach (var art in Model)
25 {
26 i++;
27 if (i == 1)
28 {
29 %>
30 <tr class=“tbhead“>
31 <%=Html.DisplayFor(m => art, “header“, “di“)%>
32
33 <th>操作</th>
34 </tr>
35 <%} %>
36 <tr>
37 <%=Html.DisplayFor(m => art, “rows“, “di“)%>
38 <td style=“width:60px“>
39 <%: Html.ActionLink(“编辑“, “Edit“, new { id = art.Id })%> <%: Html.ActionLink(“删除“, “Delete“, new { id = art.Id }, new { onclick = “return confirm(‘你确定要删除 吗?’)“ })%>
40 </td>
41 </tr>
42 <%} %>
43 </table>
44
45 <div class=“page“>
46 <%=ViewData[“page“] %>
47 </div>
48 </div>
49 </div>
50 </asp:Content>
51
编辑页面Edit.aspx
代码
<%@ Page Title=“” Language=“C#“ MasterPageFile=“~/Views/Shared/Admin.Master“ Inherits=“System.Web.Mvc.ViewPage<dynamic>“ %>
<asp:Content ID=“Content1“ ContentPlaceHolderID=“TitleContent“ runat=“server“>
Edit
</asp:Content> <asp:Content ID=“Content2“ ContentPlaceHolderID=“MainContent“ runat=“server“>
<%Html.EnableClientValidation(); %>
<div id=“headImg“>
<img src=“/content/adminimages/01.gif“ alt=“” /><%:ViewData[“NavTitle“]%>
</div>
<div id=“cont2“>
<div class=“form clearfix“>
<div>
<%: Html.ActionLink(“返回列表“, “Index“) %>
</div>
<% using (Html.BeginForm()) {%>
<%: Html.ValidationSummary(true) %> <%=Html.EditorForModel() %>
<p class=“submit“ style=“margin-left:100px;“>
<input type=“submit“ value=“保存“ />
</p> <% } %> </div>
</div>
</asp:Content>
Edit
</asp:Content> <asp:Content ID=“Content2“ ContentPlaceHolderID=“MainContent“ runat=“server“>
<%Html.EnableClientValidation(); %>
<div id=“headImg“>
<img src=“/content/adminimages/01.gif“ alt=“” /><%:ViewData[“NavTitle“]%>
</div>
<div id=“cont2“>
<div class=“form clearfix“>
<div>
<%: Html.ActionLink(“返回列表“, “Index“) %>
</div>
<% using (Html.BeginForm()) {%>
<%: Html.ValidationSummary(true) %> <%=Html.EditorForModel() %>
<p class=“submit“ style=“margin-left:100px;“>
<input type=“submit“ value=“保存“ />
</p> <% } %> </div>
</div>
</asp:Content>
添加记录页面Add.aspx
代码
<%@ Page Title=“” Language=“C#“ MasterPageFile=“~/Views/Shared/Admin.Master“ Inherits=“System.Web.Mvc.ViewPage<dynamic>“ %>
<asp:Content ID=“Content1“ ContentPlaceHolderID=“TitleContent“ runat=“server“>
Add
</asp:Content> <asp:Content ID=“Content2“ ContentPlaceHolderID=“MainContent“ runat=“server“>
<%Html.EnableClientValidation(); %> <div id=“headImg“>
<img src=“/content/adminimages/01.gif“ alt=“” /><%:ViewData[“NavTitle“]%>
</div>
<div id=“cont2“>
<div class=“form clearfix“>
<div>
<%: Html.ActionLink(“返回列表“, “Index“) %>
</div>
<% using (Html.BeginForm()) {%>
<%: Html.ValidationSummary(true) %> <%=Html.EditorForModel() %>
<p class=“submit“ style=“margin-left:100px;“>
<input type=“submit“ value=“保存“ />
</p> <% } %> </div>
</div> </asp:Content>
Add
</asp:Content> <asp:Content ID=“Content2“ ContentPlaceHolderID=“MainContent“ runat=“server“>
<%Html.EnableClientValidation(); %> <div id=“headImg“>
<img src=“/content/adminimages/01.gif“ alt=“” /><%:ViewData[“NavTitle“]%>
</div>
<div id=“cont2“>
<div class=“form clearfix“>
<div>
<%: Html.ActionLink(“返回列表“, “Index“) %>
</div>
<% using (Html.BeginForm()) {%>
<%: Html.ValidationSummary(true) %> <%=Html.EditorForModel() %>
<p class=“submit“ style=“margin-left:100px;“>
<input type=“submit“ value=“保存“ />
</p> <% } %> </div>
</div> </asp:Content>
————————————————————————————
————————————————————————