Asp.net mvc 2 in action 笔记-3 Areas AJAX AutoMapper
Areas
第21、22章
分组管理Controller
关键点
在Global.asax.cs增加AreaRegistration.RegisterAllAreas();
重载AreaRegistration 的RegisterArea函数,定义路由
其他都和普通的Controller一样,每个Area下的目录也包含MVC目录
方便移植的Area:程序逻辑和视图等打包 在一个库文件中,其他我web引用即可使用,提供了例子实现。
AJAX
第12、27章
MVC项目默认集成了JQuery库和Microsoft AJAX库(最初的Atlas)
AJAX Helper
System.Web.Mvc ..::. AjaxHelper
页面必须包含脚本
<script src="http://www.cnblogs.com/Scripts/jquery-1.3.2.js" type="text/javascript"></script>
<script src="http://www.cnblogs.com/Scripts/MicrosoftAjax.js" type="text/javascript"></script>
<script src="http://www.cnblogs.com/Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script>
<ul id="comments">
</ul>
<% using(Ajax.BeginForm("AddComment", new AjaxOptions
{
HttpMethod = "POST",
UpdateTargetId = "comments",
InsertionMode = InsertionMode.InsertAfter
})) { %>
<%= Html.TextArea("Comment", new{rows=5, cols=50}) %>
<button type="submit">Add Comment</button>
<% } %>
<h3>Ajax.Link</h3>
<%= Ajax.ActionLink("Show the privacy Policy", "PrivacyPolicy",
new AjaxOptions{InsertionMode = InsertionMode.Replace, UpdateTargetId = "privacy"}) %>
<div id="privacy"></div>
可见以上的封装简化了AJAX的处理
控制器的处理可以使用
ContentResult JsonResult等返回结果
JQuery
这个图书 Juery In Action 描述的十分详细
AutoMapper
第18章
应用场景:实现Domain Model向Presentation Model的转换[例如Domain Model的对象关系较多等等情况],比如数据类型的转换和格式化等等都可以使用这个处理,以减少View处理的复杂性
初始化
public class AutoMapperConfiguration
{
public static void Configure()
{
Mapper.Initialize(x => x.AddProfile<ExampleProfile>());
}
Global.asax.cs:
protected void Application_Start()
{
AutoMapperConfiguration.Configure();
AutoMapper profiles
a profile is a collection of type-mapping definitions, including rules that apply to all maps defined in the profile.
public class ExampleProfile : Profile
{
protected override string ProfileName
{
get { return "ViewModel"; }
}
protected override void Configure()
{
AddFormatter<HtmlEncoderFormatter>();
ForSourceType<Name>().AddFormatter<NameFormatter>();
ForSourceType<decimal>()
.AddFormatExpression(context =>
((decimal)context.SourceValue).ToString("c"));
CreateMap<Customer, CustomerInfo>()
.ForMember(x => x.ShippingAddress, opt =>
{
opt.AddFormatter<AddressFormatter>();
opt.SkipFormatter<HtmlEncoderFormatter>();
});
CreateMap<Customer, CustomerInput>().AfterMap((c, ci) => CreateSelectList(c, ci));
}
private static void CreateSelectList(Customer customer, CustomerInput input)
{
IEnumerable<CustomerStatus> allCustomerStatuses = Enumeration.GetAll<CustomerStatus>();
int selectedValue = customer.Status.Value;
input.AllStatuses = new SelectList(allCustomerStatuses, "Value", "DisplayName", selectedValue);
}
}
应用
定义一个Action Filter
public class AutoMapModelAttribute : ActionFilterAttribute
{
private readonly Type _destType;
private readonly Type _sourceType;
public AutoMapModelAttribute(Type sourceType, Type destType)
{
_sourceType = sourceType;
_destType = destType;
}
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
object model = filterContext.Controller.ViewData.Model;
object viewModel = Mapper.Map(model, _sourceType, _destType);
filterContext.Controller.ViewData.Model = viewModel;
}
}
在具体的Action上述性标注即可实现转换
[AutoMapModel(typeof(IEnumerable<Customer>), typeof(IEnumerable<CustomerInfo>))]
视图可以使用转换后的类型,如上例的CustomerInfo