MVC如何绑定复杂类型的页面Model
2014-12-09 20:34
281 查看
页面绑定简单类型的model时,可以在控制器里直接拿到页面绑定的model中post回来的值,但是如果一个model是复杂类型,比如model中有另一个自定义对象,这样的model就无法在控制器里面直接获取到值了。
这时候就只能按层次的获取每个层次的值,具体如何实现代码如下:
然后
这时候就只能按层次的获取每个层次的值,具体如何实现代码如下:
public class SimpleModelBinder:IModelBinder { public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { object model = this.GetModel(controllerContext, bindingContext.ModelType, bindingContext.ValueProvider, bindingContext.ModelName); if (bindingContext.FallbackToEmptyPrefix && null == model) { model = this.GetModel(controllerContext, bindingContext.ModelType, bindingContext.ValueProvider, ""); } return model; } public object GetComplexModel(ControllerContext controllerContex,Type modelType,IValueProvider valueProvider,string prefix) { object oModel = CreateModel(modelType); PropertyDescriptorCollection pdc = TypeDescriptor.GetProperties(modelType); foreach (PropertyDescriptor pd in pdc) { if (pd.IsReadOnly) { continue; } string key = string.IsNullOrEmpty(prefix) ? pd.Name : prefix + "." + pd.Name; pd.SetValue(oModel,GetModel(controllerContex,pd.PropertyType,valueProvider,key)); } return oModel; } public virtual object GetModel(ControllerContext controllerContext,Type modelType,IValueProvider valueProvider,string key) { if (!valueProvider.ContainsPrefix(key)) { return null; } ModelMetadata modelMeatadata = ModelMetadataProviders.Current.GetMetadataForType(null, modelType); if (!modelMeatadata.IsComplexType) { return valueProvider.GetValue(key).ConvertTo(modelType); } if(modelMeatadata.IsComplexType) { return GetComplexModel(controllerContext, modelType, valueProvider, key); } return null; } private object CreateModel(Type modelType) { Type type = modelType; if (modelType.IsGenericType) { Type genericTypeDefinition = modelType.GetGenericTypeDefinition(); if (genericTypeDefinition == typeof(IDictionary<,>)) { type = typeof(Dictionary<,>).MakeGenericType(modelType.GetGenericArguments()); } else if (((genericTypeDefinition == typeof(IEnumerable<>)) || (genericTypeDefinition == typeof(ICollection<>))) || (genericTypeDefinition == typeof(IList<>))) { type = typeof(List<>).MakeGenericType( modelType.GetGenericArguments()); } } return Activator.CreateInstance(type); } }
然后
[ModelBinder(typeof(SimpleModelBinder))] public class RoleModel { public string RoleName { set; get; } public string RoleDescription { set; get; } public string RoleExistId { set; get; } public UserMangerModle userManagerModel { set; get; } ...... } 就可以通过遍历各层属性就行复杂类型值的获取了。。。
相关文章推荐
- 如何应用Asp.Net Mvc内建功能(DefaultModelBinder)实现简单类型、复杂类型、集合类型,以及字典类型的自动绑定
- 通过实例模拟ASP.NET MVC的Model绑定机制:简单类型+复杂类型
- 通过实例模拟ASP.NET MVC的Model绑定机制:简单类型+复杂类型
- 【原创】Asp.net MVC学习笔记之-基于类型来绑定Model的属性
- asp.net mvc源码分析-DefaultModelBinder 自定义的普通数据类型的绑定和验证
- spring3mvc如何把checkbox的值绑定到model对象的int数据类型
- MVC 传递复杂类型绑定模型
- knockout里面一个页面需要绑定多个ViewModel如何处理
- MVC view页面需要多个model,复杂网页的处理
- mvc4强类型绑定提交form不想刷新页面的处理
- ASP.NET MVC:如何使用BindAttribute来定制Model绑定的Properties?
- asp.net mvc源码分析-DefaultModelBinder 自定义的普通数据类型的绑定和验证
- mvc表单如何绑定bool类型的属性或变量
- MVC复杂类型的模型绑定
- mvc4强类型绑定提交form不想刷新页面的处理
- asp.net mvc源码分析-DefaultModelBinder 自定义的普通数据类型的绑定和验证
- spring3mvc如何把checkbox的值绑定到model对象的int数据类型
- 如何使用jQuery向asp.net Mvc传递复杂json数据-ModelBinder篇
- Model绑定机制1:简单类型+复杂类型
- C--如何定义复杂的类型声明