返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, Ajax, Unit Test
2009-05-21 09:03
751 查看
[索引页]
[源码下载]
[align=center]返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, Ajax, Unit Test[/align]
作者:webabcd
介绍
asp.net mvc 之 Action Filter, UpdateModel, ModelBinder, Ajax, Unit Test
Action Filter - 在 Controller 层对信息做过滤。如何实现自定义的 Action Filter
UpdateModel - 根据参数自动为对象的属性赋值
ModelBinder - 定义如何绑定 Model,DefaultModelBinder 实现了 IModelBinder ,其可以根据名称自动将参数赋值到对象对应的属性上
Ajax - 在 asp.net mvc 中使用 ajax
Unit Test - 在 asp.net mvc 中使用单元测试
示例
1、asp.net mvc 自带的 Action Filter 的演示
FilterDemoController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using MVC.Models;
namespace MVC.Controllers
自定的 Action Filter 的实现
MyFilter.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MVC
Details.aspx
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Details
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<%= Html.AntiForgeryToken() %>
<h2>
Details</h2>
<p>
<strong>ProductID:</strong>
<%= Html.Encode(Model.ProductID) %>
</p>
<p>
<strong>ProductName:</strong>
<%= Html.Encode(Model.ProductName) %>
</p>
<p>
<input type="submit" name="btnSubmit" value="submit" />
</p>
</asp:Content>
2、应用 UpdateModel 的 Demo
UpdateModelController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
namespace MVC.Controllers
Details.aspx
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Details
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>
Details</h2>
<p>
<strong>UserId:</strong>
<%= Model.ID %>
</p>
<p>
<strong>Name:</strong>
<%= Model.Name%>
</p>
<p>
<strong>Age:</strong>
<%= Model.Age%>
</p>
</asp:Content>
3、演示什么是 ModelBinder
ModelBinderController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
namespace MVC.Controllers
Details.aspx
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Details
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>
Details</h2>
<p>
<strong>UserId:</strong>
<%= model.ID %>
</p>
<p>
<strong>Name:</strong>
<%= model.Name %>
</p>
<p>
<strong>Age:</strong>
<%= model.Age %>
</p>
<p>
<strong>error:</strong>
<%= ViewData["errorMsg"] %>
</p>
</asp:Content>
4、使用 ajax 的 Demo
<p>
Ajax
<script src="http://www.cnblogs.com/Scripts/MicrosoftAjax.debug.js" type="text/javascript"></script>
<script src="http://www.cnblogs.com/Scripts/MicrosoftMvcAjax.debug.js" type="text/javascript"></script>
<br />
<!-- AjaxHelper 简要说明 -->
<%= Ajax.ActionLink(
"ProductId 为 1 的详情页",
"Details",
"Product",
new { id = 1 },
new AjaxOptions { UpdateTargetId = "ajax" }
)
%>
</p>
<div id="ajax" />
5、在 VS 中做单元测试
ProductControllerTest.cs
using MVC.Controllers;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.VisualStudio.TestTools.UnitTesting.Web;
using System.Web.Mvc;
using MVC.Models;
using System.Collections.Generic;
namespace MVC.Tests
OK
[源码下载]
[源码下载]
[align=center]返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, Ajax, Unit Test[/align]
作者:webabcd
介绍
asp.net mvc 之 Action Filter, UpdateModel, ModelBinder, Ajax, Unit Test
Action Filter - 在 Controller 层对信息做过滤。如何实现自定义的 Action Filter
UpdateModel - 根据参数自动为对象的属性赋值
ModelBinder - 定义如何绑定 Model,DefaultModelBinder 实现了 IModelBinder ,其可以根据名称自动将参数赋值到对象对应的属性上
Ajax - 在 asp.net mvc 中使用 ajax
Unit Test - 在 asp.net mvc 中使用单元测试
示例
1、asp.net mvc 自带的 Action Filter 的演示
FilterDemoController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using MVC.Models;
namespace MVC.Controllers
自定的 Action Filter 的实现
MyFilter.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MVC
Details.aspx
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Details
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<%= Html.AntiForgeryToken() %>
<h2>
Details</h2>
<p>
<strong>ProductID:</strong>
<%= Html.Encode(Model.ProductID) %>
</p>
<p>
<strong>ProductName:</strong>
<%= Html.Encode(Model.ProductName) %>
</p>
<p>
<input type="submit" name="btnSubmit" value="submit" />
</p>
</asp:Content>
2、应用 UpdateModel 的 Demo
UpdateModelController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
namespace MVC.Controllers
Details.aspx
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Details
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>
Details</h2>
<p>
<strong>UserId:</strong>
<%= Model.ID %>
</p>
<p>
<strong>Name:</strong>
<%= Model.Name%>
</p>
<p>
<strong>Age:</strong>
<%= Model.Age%>
</p>
</asp:Content>
3、演示什么是 ModelBinder
ModelBinderController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
namespace MVC.Controllers
Details.aspx
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Details
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>
Details</h2>
<p>
<strong>UserId:</strong>
<%= model.ID %>
</p>
<p>
<strong>Name:</strong>
<%= model.Name %>
</p>
<p>
<strong>Age:</strong>
<%= model.Age %>
</p>
<p>
<strong>error:</strong>
<%= ViewData["errorMsg"] %>
</p>
</asp:Content>
4、使用 ajax 的 Demo
<p>
Ajax
<script src="http://www.cnblogs.com/Scripts/MicrosoftAjax.debug.js" type="text/javascript"></script>
<script src="http://www.cnblogs.com/Scripts/MicrosoftMvcAjax.debug.js" type="text/javascript"></script>
<br />
<!-- AjaxHelper 简要说明 -->
<%= Ajax.ActionLink(
"ProductId 为 1 的详情页",
"Details",
"Product",
new { id = 1 },
new AjaxOptions { UpdateTargetId = "ajax" }
)
%>
</p>
<div id="ajax" />
5、在 VS 中做单元测试
ProductControllerTest.cs
using MVC.Controllers;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.VisualStudio.TestTools.UnitTesting.Web;
using System.Web.Mvc;
using MVC.Models;
using System.Collections.Generic;
namespace MVC.Tests
OK
[源码下载]
相关文章推荐
- 返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, Ajax, Unit Test
- 返璞归真asp.net mvc 1.0(5) - Action Filter, UpdateModel, ModelBinder, Ajax, Unit Test
- 返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, Ajax, Unit Test
- 返璞归真asp.net mvc 1.0(5) - Action Filter, UpdateModel, ModelBinder, Ajax, Unit Test【转】
- asp.net mvc 1.0(5) - Action Filter, UpdateModel, ModelBinder, Ajax, Unit Test(非原创)
- asp.net mvc 1.0(5) - Action Filter, UpdateModel, ModelBinder, Ajax, Unit Test(非原创)
- 深入分析 ASP.NET Mvc 1.0 – 4. 使用ModelBinder绑定Action的参数
- ASP.NET MVC中默认Model Binder绑定Action参数为List、Dictionary等集合的实例
- ASP.NET MVC 利用ActionFilterAttribute来判断用户是否登陆等等
- ASP.NET MVC中如何以ajax的方式在View和Action中传递数据
- ASP.NET MVC 通过ActionFilter 实现AOP设计 示例
- ASP.NET MVC 使用TryUpdateModel 更新的技巧
- ASP.NET MVC: 使用自定义 ModelBinder
- asp.net mvc源码分析-DefaultModelBinder 自定义的普通数据类型的绑定和验证
- ASP.NET MVC:如何实现一个自己的ModelBinder?
- ModelBinder——ASP.NET MVC Model绑定的核心
- ASP.NET MVC & EF 构建智能查询 二、模型的设计与ModelBinder
- 使用ModelBinder绑定IPrincipal (User)简化ASP.NET MVC单元测试
- 转:ASP.NET MVC利用TryUpdateModel来做资料更新 (一)
- ASP.NET MVC 入门10、Action Filter 与 内置的Filter实现(实例-防盗链)