MVC如何綁定複雜類型的頁面Model

來源:互聯網
上載者:User

標籤:des   style   blog   io   ar   color   os   sp   for   

  頁面綁定簡單類型的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; }......}
就可以通過遍曆各層屬性就行複雜類型值的擷取了。。。

MVC如何綁定複雜類型的頁面Model

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.