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引用即可使用,提供了例子实现。

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

目录
打赏
0
0
0
0
9
分享
相关文章
ASP.NET MVC 教程
ASP.NET 是一个使用 HTML、CSS、JavaScript 和服务器脚本创建网页和网站的开发框架。
105 7
【Azure Developer】使用.Net Core解析JSON的笔记
【Azure Developer】使用.Net Core解析JSON的笔记
ASP.NET MVC 迅速集成 SignalR
ASP.NET MVC 迅速集成 SignalR
138 0
ASP.NET CORE 3.1 MVC“指定的网络名不再可用\企图在不存在的网络连接上进行操作”的问题解决过程
ASP.NET CORE 3.1 MVC“指定的网络名不再可用\企图在不存在的网络连接上进行操作”的问题解决过程
272 0
ASP.NET MVC WebApi 接口返回 JOSN 日期格式化 date format
ASP.NET MVC WebApi 接口返回 JOSN 日期格式化 date format
95 0
|
8月前
|
Asp.Net Core 使用X.PagedList.Mvc.Core分页 & 搜索
Asp.Net Core 使用X.PagedList.Mvc.Core分页 & 搜索
226 0
ASP.NET Core Web中使用AutoMapper进行对象映射
ASP.NET Core Web中使用AutoMapper进行对象映射
C# .NET面试系列六:ASP.NET MVC
<h2>ASP.NET MVC #### 1. MVC 中的 TempData\ViewBag\ViewData 区别? 在ASP.NET MVC中,TempData、ViewBag 和 ViewData 都是用于在控制器和视图之间传递数据的机制,但它们有一些区别。 <b>TempData:</b> 1、生命周期 ```c# TempData 的生命周期是短暂的,数据只在当前请求和下一次请求之间有效。一旦数据被读取,它就会被标记为已读,下一次请求时就会被清除。 ``` 2、用途 ```c# 主要用于在两个动作之间传递数据,例如在一个动作中设置 TempData,然后在重定向到另
432 5
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等