返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, Ajax, Unit Test

原文:返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, Ajax, Unit Test

[索引页]
[源码下载]


返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, Ajax, Unit Test


作者: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
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Testusing System;
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
using System.Collections.Generic;
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
using System.Linq;
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
using System.Web;
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
using System.Web.Mvc;
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
using System.Web.Mvc.Ajax;
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
using MVC.Models;
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
namespace MVC.Controllers
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
{
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test    
// HandleError - 出现异常时跳转到 Error.aspx(先在指定的 view 文件夹找,找不到再在 Shared 文件夹下找)
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test    
// 需要 web.config 配置 - <customErrors mode="On" />
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
    [HandleError]
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test    
public class FilterDemoController : Controller
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test    
{
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        
// 每一个 Filter 都有一个 Order 属性,其用来指定 Filter 的执行顺序
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        
// [NonAction] - 当前方法为普通方法,不解析为 Action
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        
// [AcceptVerbs(HttpVerbs)] - 调用该 Action 的 http 方法
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        
// HttpVerbs 枚举成员如下: HttpVerbs.Get, HttpVerbs.Post, HttpVerbs.Put, HttpVerbs.Delete, HttpVerbs.Head
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test

返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        ProductSystem ps 
= new ProductSystem();
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        
// ActionName() - Action 的名称。默认值同方法名称
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
        [ActionName("Product")]
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        
// ValidateInput() - 相当于 @ Page 的 ValidateRequest, 用于验证请求中是否存在危险代码。可防止 XSS 攻击。默认值为 true
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
        [ValidateInput(false)]
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        
public ActionResult Details(int id)
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        
{
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test            var product 
= ps.GetProduct(id);
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test            
return View("Details", product);
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        }

返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        
// ValidateAntiForgeryToken() - 避免 CSRF 攻击(需要视图页面中使用 Html.AntiForgeryToken())。原理:生成一个随机字符串,将其同时写入 cookie 和 hidden,当 form 提交到 action 时,验证二者是否相等并且验证提交源是否是本站页面(详查 asp.net mvc 的源代码)
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        
// 拼 sql 的时候防止 sql 注入:使用 @Parameter 的方式
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
        [ValidateAntiForgeryToken()]
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        
public ActionResult ValidateAntiForgeryTokenTest()
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        
{
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test            
return Content("ValidateAntiForgeryToken");
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        }

返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        
// OutputCache() - 缓存。Duration - 缓存秒数。VaryByParam - none, *, 多个参数用逗号隔开
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
        [OutputCache(Duration = 10, VaryByParam = "none")]
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        
// [OutputCache(CacheProfile = "MyCache")] - 通过配置文件对缓存做设置。可以参看 http://www.cnblogs.com/webabcd/archive/2007/02/15/651419.html
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
        public ActionResult OutputCacheDemo()
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        
{
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test            
return Content(DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        }

返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        
public ActionResult HandleErrorDemo()
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        
{
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test            
throw new Exception("HandleErrorDemo");
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        }

返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        
// Authorize() - 验证。走的是 membership
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
        [Authorize(Users = "user")]
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        
// [Authorize(Roles = "role")]
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        
// Request.IsAuthenticated - 返回一个 bool 值,用于指示请求是否通过了验证
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
        public ActionResult AuthorizeDemo()
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        
{
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test            
return Content("Authorize");
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        }

返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        
// 自定义的 Action Filter 的 Demo
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
        [MyFilter()]
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        
public ActionResult MyFilterDemo()
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        
{
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test            
return Content("MyFilterDemo" + "<br />");
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        }

返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test    }

返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test}

返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test

自定的 Action Filter 的实现
MyFilter.cs
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Testusing System;
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
using System.Collections.Generic;
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
using System.Linq;
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
using System.Web;
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
using System.Web.Mvc;
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
namespace MVC
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
{
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test    
/// <summary>
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test    
/// 自定义的 Action Filter,需要继承 ActionFilterAttribute
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test    
/// </summary>

返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test    public class MyFilter : ActionFilterAttribute
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test    
{
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        
public override void OnActionExecuting(ActionExecutingContext filterContext)
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        
{
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test            HttpContext.Current.Response.Write(
"OnActionExecuting" + "<br />");
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        }

返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        
public override void OnActionExecuted(ActionExecutedContext filterContext)
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        
{
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test            HttpContext.Current.Response.Write(
"OnActionExecuted" + "<br />");
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        }

返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        
public override void OnResultExecuting(ResultExecutingContext filterContext)
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        
{
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test            HttpContext.Current.Response.Write(
"OnResultExecuting" + "<br />");
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        }

返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        
public override void OnResultExecuted(ResultExecutedContext filterContext)
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        
{
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test            HttpContext.Current.Response.Write(
"OnResultExecuted" + "<br />");
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        }

返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test    }

返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test}

返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test

Details.aspx
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MVC.Models.Products>" %>
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test    Details
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
</asp:Content>
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test    
<% Html.BeginForm("ValidateAntiForgeryTokenTest""FilterDemo"); %>
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test    
<%= Html.AntiForgeryToken() %>
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test    
<h2>
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        Details
</h2>
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test    
<p>
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        
<strong>ProductID:</strong>
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        
<%= Html.Encode(Model.ProductID) %>
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test    
</p>
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test    
<p>
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        
<strong>ProductName:</strong>
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        
<%= Html.Encode(Model.ProductName) %>
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test    
</p>
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test    
<p>
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        
<input type="submit" name="btnSubmit" value="submit" />
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test    
</p>
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test    
<% Html.EndForm(); %>
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
</asp:Content>
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test


2、应用 UpdateModel 的 Demo
UpdateModelController.cs
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Testusing System;
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
using System.Collections.Generic;
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
using System.Linq;
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
using System.Web;
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
using System.Web.Mvc;
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
using System.Web.Mvc.Ajax;
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
namespace MVC.Controllers
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
{
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test    
public class UpdateModelController : Controller
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test    
{
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        
public ActionResult Details(string name, int age)
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        
{
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test            User user 
= new User();
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test            
// UpdateModel() 和 TryUpdateModel() - 系统根据参数自动为对象的属性赋值
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test

返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test            
base.UpdateModel(user); // 失败抛异常
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test            
// base.TryUpdateModel(user); // 失败不抛异常
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test

返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test            ViewData.Model 
= user;
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test            
return View();
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        }

返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test    }

返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test}

返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test

Details.aspx
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MVC.Controllers.User>" %>
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test    Details
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
</asp:Content>
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test    
<h2>
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        Details
</h2>
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test    
<p>
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        
<strong>UserId:</strong>
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        
<%= Model.ID %>
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test    
</p>
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test    
<p>
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        
<strong>Name:</strong>
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        
<%= Model.Name%>
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test    
</p>
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test    
<p>
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        
<strong>Age:</strong>
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        
<%= Model.Age%>
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test    
</p>
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
</asp:Content>
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test


3、演示什么是 ModelBinder
ModelBinderController.cs
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Testusing System;
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
using System.Collections.Generic;
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
using System.Linq;
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
using System.Web;
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
using System.Web.Mvc;
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
using System.Web.Mvc.Ajax;
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
namespace MVC.Controllers
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
{
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test    
public class ModelBinderController : Controller
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test    
{
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        
/// <summary>
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        
/// 路由过来的或者参数过来的,会自动地赋值到对象的对应的属性上
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        
/// 做这个工作的就是实现了 IModelBinder 接口的 DefaultModelBinder
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        
/// </summary>

返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        public ActionResult Details(User user)
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        
{
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test            
base.ViewData.Model = user;
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test            
// ModelState - 保存 Model 的状态,包括相应的错误信息等
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
            if (!base.ModelState.IsValid)
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test            
{
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test                
foreach (var state in ModelState)
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test                
{
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test                    
if (!base.ModelState.IsValidField(state.Key))
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test                        ViewData[
"errorMsg"= state.Key + "/" + state.Value.Value.AttemptedValue + "<br />";
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test                }

返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test            }

返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test            
return View();
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        }

返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test    }

返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test}

返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test

Details.aspx
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test    Details
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
</asp:Content>
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test    
<h2>
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        Details
</h2>
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test    
<% var model = ((MVC.Controllers.User)ViewData.Model); %>
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test    
<p>
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        
<strong>UserId:</strong>
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        
<%= model.ID %>
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test    
</p>
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test    
<p>
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        
<strong>Name:</strong>
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        
<%= model.Name %>
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test    
</p>
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test    
<p>
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        
<strong>Age:</strong>
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        
<%= model.Age %>
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test    
</p>
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test    
<p>
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        
<strong>error:</strong>
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        
<%= ViewData["errorMsg"%>
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test    
</p>
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
</asp:Content>
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test


4、使用 ajax 的 Demo
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test    <p>
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        Ajax
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        
<script src="http://www.cnblogs.com/Scripts/MicrosoftAjax.debug.js" type="text/javascript"></script>
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        
<script src="http://www.cnblogs.com/Scripts/MicrosoftMvcAjax.debug.js" type="text/javascript"></script>
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test                       
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        
<br />
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        
<!-- AjaxHelper 简要说明 -->
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        
<%= Ajax.ActionLink(
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test                
"ProductId 为 1 的详情页",
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test                
"Details",
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test                
"Product",
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test                
new { id = 1 },
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test                
new AjaxOptions { UpdateTargetId = "ajax" }
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test            )
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        
%> 
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test    
</p>
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test    
<div id="ajax" />
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test


5、在 VS 中做单元测试
ProductControllerTest.cs
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Testusing MVC.Controllers;
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
using Microsoft.VisualStudio.TestTools.UnitTesting;
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
using Microsoft.VisualStudio.TestTools.UnitTesting.Web;
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
using System.Web.Mvc;
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
using MVC.Models;
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
using System.Collections.Generic;
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
namespace MVC.Tests
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
{
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test    
/// <summary>
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test    
///这是 ProductControllerTest 的测试类,旨在
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test    
///包含所有 ProductControllerTest 单元测试
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test    
///</summary>

返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test    [TestClass()]
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test    
public class ProductControllerTest
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test    
{
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        
private TestContext testContextInstance;
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        
/// <summary>
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        
///获取或设置测试上下文,上下文提供
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        
///有关当前测试运行及其功能的信息。
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        
///</summary>

返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        public TestContext TestContext
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        
{
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test            
get
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test            
{
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test                
return testContextInstance;
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test            }

返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test            
set
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test            
{
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test                testContextInstance 
= value;
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test            }

返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        }

返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        
附加测试属性
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        
/// <summary>
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        
///Index 的测试
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        
///</summary>

返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        // TODO: 确保 UrlToTest 属性指定一个指向 ASP.NET 页的 URL(例如,
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        
// http://返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test/Default.aspx)。这对于在 Web 服务器上执行单元测试是必需的,
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        
//无论要测试页、Web 服务还是 WCF 服务都是如此。
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
        [TestMethod()]
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        [HostType(
"ASP.NET")]
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        [AspNetDevelopmentServerHost(
"C:\\MVC\\MVC""/")]
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        [UrlToTest(
"http://localhost:2005/")]
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        
public void IndexTest()
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        
{
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test            ProductController target 
= new ProductController(); // TODO: 初始化为适当的值
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
            int pageIndex = 0// TODO: 初始化为适当的值
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test

返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test            ViewResult actual;
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test            actual 
= target.Index(pageIndex) as ViewResult;
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test            Assert.AreNotEqual(actual.ViewData.Model 
as List<Products>null);
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test        }

返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test    }

返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test}

返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test
返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, 
Ajax, Unit Test


OK
[源码下载]

返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, Ajax, Unit Test,布布扣,bubuko.com

返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, Ajax, Unit Test

上一篇:unix:/tmp/php-cgi.sock


下一篇:Android学习之Animation(一)