/MVC3ControlsToolkit/Controls/Bindings/ModelTranslator.cs
C# | 253 lines | 207 code | 33 blank | 13 comment | 32 complexity | 7dd7b81d53b5581ccc57b324549e55e8 MD5 | raw file
- /* ****************************************************************************
- *
- * Copyright (c) Francesco Abbruzzese. All rights reserved.
- * francesco@dotnet-programming.com
- * http://www.dotnet-programming.com/
- *
- * This software is subject to the the license at http://mvccontrolstoolkit.codeplex.com/license
- * and included in the license.txt file of this distribution.
- *
- * You must not remove this notice, or any other, from this software.
- *
- * ***************************************************************************/
- using System;
- using System.Collections.Generic;
- using System.Collections;
- using System.Linq;
- using System.Text;
- using MVCControlsToolkit.Core;
- using MVCControlsToolkit.Controls;
- using System.Web.Script.Serialization;
- using System.Reflection;
- using System.ComponentModel.DataAnnotations;
- using System.Text.RegularExpressions;
- using System.Collections.ObjectModel;
- using MVCControlsToolkit.DataAnnotations;
- using System.Web.Mvc;
-
- namespace MVCControlsToolkit.Controls.Bindings
- {
- internal class ModelTranslatorBase: IUpdateModelState, IRebindChildren
- {
- protected string prefix;
- protected System.Web.Mvc.ModelStateDictionary modelState;
- [AllowHtml]
- public string JSonModel { get; set; }
- protected void Validate(object x, object father, string propertyName, string currPrefix)
- {
- if (x == null) return;
- if (x is IConvertible)
- {
- if (father == null) return;
- ValidationContext ctx = new ValidationContext(father, null, null);
- ctx.MemberName = propertyName;
- List<ValidationResult> errors = new List<ValidationResult>();
- bool success = Validator.TryValidateProperty(x, ctx, errors);
- if (!success)
- {
- foreach (ValidationResult vs in errors)
- {
- modelState.AddModelError(currPrefix, vs.ErrorMessage);
- }
- }
-
- }
- else if (x is IEnumerable)
- {
- int i = 0;
- foreach (object y in x as IEnumerable)
- {
- Validate(y, null, null,
- currPrefix +
- string.Format("[{0}]", i)
- );
- i++;
- }
- }
- else
- {
- Type currType = x.GetType();
- if (currType.IsClass)
- {
- List<ValidationResult> errors = new List<ValidationResult>();
- bool success = Validator.TryValidateObject(x, new ValidationContext(x, null, null), errors, true);
- ValidationAttribute[] attrs = currType.GetCustomAttributes(typeof(ValidationAttribute), true) as ValidationAttribute[];
-
- foreach (ValidationAttribute attr in attrs)
- {
- ValidationResult vs = attr.GetValidationResult(x, new ValidationContext(x, null, null));
- if (vs != null && vs != ValidationResult.Success)
- modelState.AddModelError(currPrefix, vs.ErrorMessage);
- }
-
- foreach (PropertyInfo prop in currType.GetProperties())
- {
- ServerBindAttribute[] goServer = Attribute.GetCustomAttributes(prop, typeof(ServerBindAttribute)) as ServerBindAttribute[];
- if (goServer != null && goServer.Length > 0)
- {
- ModelBindingContext newBindingContext = new ModelBindingContext();
- newBindingContext.ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType((() => null), prop.PropertyType);
- newBindingContext.ModelMetadata.IsRequired = false;
-
- newBindingContext.ModelName = BasicHtmlHelper.AddField(currPrefix, prop.Name);
- newBindingContext.ModelState = currentBindingContext.ModelState;
- newBindingContext.PropertyFilter = currentBindingContext.PropertyFilter;
- newBindingContext.ValueProvider = currentBindingContext.ValueProvider;
- object model = currBinder.BindModel(currentControllerContext, newBindingContext);
- prop.SetValue(x, model, new object[0]);
-
- }
- else if ((prop.PropertyType == typeof(DateTime) || prop.PropertyType == typeof(DateTime?)) && prop.CanRead && prop.CanWrite)
- {
- object dateVal = prop.GetValue(x, new object[0]);
- if (dateVal != null)
- {
- prop.SetValue(x, ((DateTime)dateVal).ToLocalTime(), new object[0]);
- }
-
- }
- Validate(prop.GetValue(x, new object[0]),
- x,
- prop.Name,
- BasicHtmlHelper.AddField(currPrefix, prop.Name));
- }
- }
- }
- }
- public void GetCurrState(string currPrefix, int updateIndex, System.Web.Mvc.ModelStateDictionary modelState)
- {
- if (currPrefix == "display") currPrefix = string.Empty;
- this.prefix = currPrefix;
- this.modelState = modelState;
- }
-
- public bool MoveState
- {
- get { return false; }
- }
- private System.Web.Mvc.IModelBinder currBinder;
- private System.Web.Mvc.ControllerContext currentControllerContext;
- System.Web.Mvc.ModelBindingContext currentBindingContext;
- public System.Web.Mvc.ControllerContext CurrentControllerContext
- {
- set { currentControllerContext = value; }
- }
-
- public System.Web.Mvc.ModelBindingContext CurrentBindingContext
- {
- set { currentBindingContext=value; }
- }
-
- public System.Web.Mvc.IModelBinder CurrBinder
- {
- set { currBinder=value; }
- }
- }
- internal class SipleModelTranslator<T> : ModelTranslatorBase, IDisplayModel
- {
-
-
- public object ExportToModel(Type targetType, params object[] context)
- {
- if (string.IsNullOrWhiteSpace(JSonModel)) return null;
- if (!targetType.IsAssignableFrom(typeof(T)))
- throw (new NotSupportedException(string.Format(ControlsResources.NotCompatibleTypes, typeof(T).FullName, targetType.FullName)));
- object result = BasicHtmlHelper.ClientDecode(JSonModel, typeof(T));
- Validate(result, null, null, prefix);
- return result;
- }
-
- public void ImportFromModel(object model, params object[] context)
- {
- JSonModel = BasicHtmlHelper.ClientEncode(model);
-
- }
-
- }
- internal class ModelTranslator<T> : ModelTranslatorBase, IDisplayModel
- {
- private static Regex dateRewrite;
- private static Regex dateRewriteOut;
- static ModelTranslator()
- {
- dateRewrite=new Regex("\""+@"\\/Date\(((-)?\d+)(?:[-+]\d+)?\)\\/"+"\"");
- dateRewriteOut = new Regex("\"" + @"\\\\/Date\(((-)?\d+)(?:[-+]\d+)?\)\\\\/" + "\"");
- }
-
-
- public object ExportToModel(Type TargetType, params object[] context)
- {
- if (string.IsNullOrWhiteSpace(JSonModel)) return null;
- if (!TargetType.IsAssignableFrom(typeof(T)))
- throw (new NotSupportedException(string.Format(ControlsResources.NotCompatibleTypes, typeof(T).FullName, TargetType.FullName)));
- JavaScriptSerializer serializer = new JavaScriptSerializer();
- //JSonModel = dateRewriteOut.Replace(JSonModel, "\"\\/Date($1)\\/\"");
- T result = serializer.Deserialize<T>(JSonModel);
- Validate(result, null, null, prefix);
- return result;
- }
-
- public void ImportFromModel(object model, params object[] context)
- {
- JavaScriptSerializer serializer = new JavaScriptSerializer();
- JSonModel = serializer.Serialize(model) ;
- JSonModel = dateRewrite.Replace(JSonModel, "new Date($1)");
- }
-
-
- }
- internal class JSONAdapter : ModelTranslatorBase, IDisplayModel
- {
- private static Regex dateRewrite;
- private static Regex dateRewriteOut;
- static JSONAdapter()
- {
- dateRewrite = new Regex("\"" + @"\\/Date\(((-)?\d+)(?:[-+]\d+)?\)\\/" + "\"");
- dateRewriteOut = new Regex("\"" + @"\\\\/Date\(((-)?\d+)(?:[-+]\d+)?\)\\\\/" + "\"");
- }
-
-
- public object ExportToModel(Type TargetType, params object[] context)
- {
- if (string.IsNullOrWhiteSpace(JSonModel)) return null;
-
- JavaScriptSerializer serializer = new JavaScriptSerializer();
- JSonModel = dateRewriteOut.Replace(JSonModel, "\"\\/Date($1)\\/\"");
- object result = serializer.Deserialize(JSonModel, TargetType);
- Validate(result, null, null, prefix);
- return result;
- }
-
- public void ImportFromModel(object model, params object[] context)
- {
- JavaScriptSerializer serializer = new JavaScriptSerializer();
- JSonModel = serializer.Serialize(model);
- JSonModel = dateRewrite.Replace(JSonModel, "new Date($1)");
- }
-
-
- }
-
- internal class ModelTranslatorLight : IDisplayModel
- {
-
- static ModelTranslatorLight()
- {
-
- }
- [AllowHtml]
- public string JSonModel { get; set; }
- public object ExportToModel(Type targetType, params object[] context)
- {
- return BasicHtmlHelper.ClientDecode(JSonModel, targetType);
- }
-
- public void ImportFromModel(object model, params object[] context)
- {
- JSonModel = BasicHtmlHelper.ClientEncode(model);
-
- }
-
-
- }
- }