使用ModelBinder处理Actin实体类型参数
2012-07-11 15:48
239 查看
一,关于DefaultModelBinder(无需额外代码就从表单提交的字符串转换实体类)
比如一个Action要传入Person类型的参数,而不是普通的字符串。
public ActionResult AddPerson(Person model)
{
return View(model);
}
MVC其实已经做了处理,如果html表单中的元素和Person类中属性名称一样,就会自动生成一个Person类,然后赋值给对应的属性字段
Html表单形式。
其中Company又是一个实体类型属性,使用company.name的表单名称来指定
这样在传入Action的时候就会自动生成Person类实例
public ActionResult AddPerson(Person model)
{
return View(model);
}
内部的实现的原理是因为有个DefaultModelBinder,这是一个默认的Model绑定转换类
二。实现一个自定义的Modelbinder并且在controller中使用
如果要实现一个自定义的ModelBinder必须要继承一个接口IModelBinder,实现这个方法BindModel
在Controller中要指定才能使用,这样在处理到这个Action的时候会先去PersonBinder 中处理接收的参数
三。在MVC中添加新的默认modelbinder
如何让自定义的ModelBinder默认执行,如果一个Modelbinder可以针对每个action都适用
比如可以自定义一个Modelbinder对所有输入的字符串都进行Trim()。最好的方式是让这个ModelBinder在每个Action中都默认执行,而不是需要每次都在
Controller中显式的指定。
这个Modelbinder需要继承DefaultModelBinder
在Global。asax文件中
这样在Controller中无需指定Modelbinder就可以执行
比如一个Action要传入Person类型的参数,而不是普通的字符串。
public ActionResult AddPerson(Person model)
{
return View(model);
}
MVC其实已经做了处理,如果html表单中的元素和Person类中属性名称一样,就会自动生成一个Person类,然后赋值给对应的属性字段
public class Person { public string name { get; set; } public string age { get; set; } public string sex { get; set; } public Company company { get; set; } } public class Company { public string name { get; set; } public string address { get; set; } }
Html表单形式。
其中Company又是一个实体类型属性,使用company.name的表单名称来指定
<form action="/home/addperson"> <input id="name" name="name" /> <input id="age" name="age" /> <input id="company.name" name="company.name"/> <input type="submit" value="submit" /> </form>
这样在传入Action的时候就会自动生成Person类实例
public ActionResult AddPerson(Person model)
{
return View(model);
}
内部的实现的原理是因为有个DefaultModelBinder,这是一个默认的Model绑定转换类
二。实现一个自定义的Modelbinder并且在controller中使用
如果要实现一个自定义的ModelBinder必须要继承一个接口IModelBinder,实现这个方法BindModel
public class PersonBinder : IModelBinder { public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { string name = controllerContext.RequestContext.HttpContext.Request["name"]; string age = controllerContext.RequestContext.HttpContext.Request["age"]; Person p = new Person() { name = name, age=age }; return p; } }
在Controller中要指定才能使用,这样在处理到这个Action的时候会先去PersonBinder 中处理接收的参数
public ActionResult AddPerson([ModelBinder(typeof(PersonBinder))]Person model) { return View(model); }
三。在MVC中添加新的默认modelbinder
如何让自定义的ModelBinder默认执行,如果一个Modelbinder可以针对每个action都适用
比如可以自定义一个Modelbinder对所有输入的字符串都进行Trim()。最好的方式是让这个ModelBinder在每个Action中都默认执行,而不是需要每次都在
Controller中显式的指定。
这个Modelbinder需要继承DefaultModelBinder
public class StringTrimBinder : DefaultModelBinder { public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { var value = base.BindModel(controllerContext, bindingContext); if (value is string) { return value.ToString().Trim(); } else { return value; } } }
在Global。asax文件中
protected void Application_Start() { AreaRegistration.RegisterAllAreas(); RegisterGlobalFilters(GlobalFilters.Filters); RegisterRoutes(RouteTable.Routes); ModelBinders.Binders.Add(typeof(string), new StringTrimBinder()); }
这样在Controller中无需指定Modelbinder就可以执行
public ActionResult AddPerson(Person model) { return View(model); }
相关文章推荐
- jmeter中使用Beanshell处理多个枚举类型参数
- WebApi系列~实际项目中如何使用HttpClient向web api发异步Get和Post请求并且参数于具体实体类型
- 关于Entity Framework更新的几种方式以及可能遇到的问题(附加类型“Model”的实体失败,因为相同类型的其他实体已具有相同的主键值)在使用 "Attach" 方法或者将实体的状态设置为 "Unchanged" 或 "Modified" 时如果图形中的任何实体具有冲突键值,则可能会发生上述行为
- MVC扩展ModelBinder使类型为DateTime的Action参数可以接收日期格式的字符串
- Effective C# 学习笔记(三十九) 使用Dynamic处理范型参数的运行时类型
- Java如何把链接中的参数在页面使用参数名字获取ModelMap model去页面显示或者处理
- springmvc 处理lsit类型的请求参数映射成实体属性
- WebApi系列~实际项目中如何使用HttpClient向web api发异步Get和Post请求并且参数于具体实体类型
- 使用C++11变长参数模板 处理任意长度、类型之参数实例
- 使用C++11变长参数模板 处理任意长度、类型之参数
- 深入分析 ASP.NET Mvc 1.0 – 4. 使用ModelBinder绑定Action的参数
- 解决 ”不允许在查询中显式构造实体类型“问题及使用其他方法实现返回 List<Model对象>或者IQueryable<Model对象>对象
- Effective C# 学习笔记(四十)使用Dynamic处理匿名类型参数
- MVC扩展ModelBinder使类型为DateTime的Action参数可以接收日期格式的字符串
- 使用Model Binder绑定Action参数字段时的取舍问题
- C++ - 使用非成员函数(non-member)处理函数的所有参数都需要类型转换
- 使用Model Binder绑定Action参数字段时的取舍问题
- InputOutput参数类型使用
- Model model,HttpServletRequest request, ModelMap map参数使用与区别
- Form表单提交参数类型与后台实体参数类型的问题