ASP.NET MVC 2扩展点之Model Binder实例分析
2010-08-05 10:01
633 查看
MVC 2的Model可以是任意一个类。
许多教程只讲“ADO.NET实体数据模型”Model1.edmx
然后连接mssql2005以上,自动生成数据模型。
这样会让初学者不能更好地理解Model与View之间的关系。
这里我详细介绍一下怎样用任意一个类做Model,
这样你也可以在MVC项目中使用Access数据库,任意数据库吧。
步骤:新建MVC项目
删除默认生成的Controller,View,我喜欢简洁,突出重点。
右击Models目录,新建Book.cs
大气象
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using MvcModelBinder.Models;
namespace MvcModelBinder
{
// 注意: 有关启用 IIS6 或 IIS7 经典模式的说明,
// 请访问 http://go.microsoft.com/?LinkId=9394801
public class MvcApplication : System.Web.HttpApplication
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // 路由名称
"{controller}/{action}/{id}", // 带有参数的 URL
new { controller = "Book", action = "List", id = UrlParameter.Optional } // 参数默认值
);
}
protected void Application_Start()
{
//据说需要这样注册一下,我不注册也不影响。不知道哪里理解不到位。
ModelBinders.Binders.Add(typeof(Book), new BookModelBinder());
AreaRegistration.RegisterAllAreas();
RegisterRoutes(RouteTable.Routes);
}
}
}
这是最终的目录结构:
本文源码:http://files.cnblogs.com/greatverve/MvcModelBinder.rar
参考:/article/4667132.html
这里这个大牛,不屑过多解释,这个工作由我来做吧。
许多教程只讲“ADO.NET实体数据模型”Model1.edmx
然后连接mssql2005以上,自动生成数据模型。
这样会让初学者不能更好地理解Model与View之间的关系。
这里我详细介绍一下怎样用任意一个类做Model,
这样你也可以在MVC项目中使用Access数据库,任意数据库吧。
步骤:新建MVC项目
删除默认生成的Controller,View,我喜欢简洁,突出重点。
右击Models目录,新建Book.cs
大气象
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using MvcModelBinder.Models;
namespace MvcModelBinder
{
// 注意: 有关启用 IIS6 或 IIS7 经典模式的说明,
// 请访问 http://go.microsoft.com/?LinkId=9394801
public class MvcApplication : System.Web.HttpApplication
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // 路由名称
"{controller}/{action}/{id}", // 带有参数的 URL
new { controller = "Book", action = "List", id = UrlParameter.Optional } // 参数默认值
);
}
protected void Application_Start()
{
//据说需要这样注册一下,我不注册也不影响。不知道哪里理解不到位。
ModelBinders.Binders.Add(typeof(Book), new BookModelBinder());
AreaRegistration.RegisterAllAreas();
RegisterRoutes(RouteTable.Routes);
}
}
}
这是最终的目录结构:
本文源码:http://files.cnblogs.com/greatverve/MvcModelBinder.rar
参考:/article/4667132.html
这里这个大牛,不屑过多解释,这个工作由我来做吧。
相关文章推荐
- 深入分析 ASP.NET Mvc 1.0 – 4. 使用ModelBinder绑定Action的参数
- Asp.net MVC DefaultModelBinder分析
- asp.net mvc源码分析-Action篇 DefaultModelBinder
- asp.net mvc源码分析-DefaultModelBinder 集合绑定
- asp.net mvc源码分析-DefaultModelBinder 自定义的普通数据类型的绑定和验证
- ASP.NET MVC 2扩展点之Model Binder
- asp.net mvc源码分析-Action篇 DefaultModelBinder
- 一起谈.NET技术,ASP.NET MVC 2扩展点之Model Binder
- asp.net mvc源码分析-Action篇 IModelBinder
- Asp.net MVC源码分析 -- 获取ModelBinder的优先级
- Asp.net MVC DefaultModelBinder分析
- asp.net mvc源码分析-Action篇 IModelBinder
- Asp.net MVC的Model Binder工作流程以及扩展方法(2) - Binder Attribute
- asp.net mvc源码分析-Action篇 IModelBinder
- Asp.net MVC的Model Binder工作流程以及扩展方法(3) - DefaultModelBinder
- Asp.net MVC的Model Binder工作流程以及扩展方法(1)
- Asp.net MVC 示例项目"Suteki.Shop"分析之---ModelBinder
- asp.net mvc源码分析-DefaultModelBinder 自定义的普通数据类型的绑定和验证
- Asp.net MVC的Model Binder工作流程以及扩展方法(1) - Custom Model Binder
- ASP.NET MVC中默认Model Binder绑定Action参数为List、Dictionary等集合的实例