/// <summary> /// Fill the Login object from a DataRow /// </summary> /// <param name="dr">DataRow containing the objects info</param> public void Fill(System.Data.DataRow dr) { System.ComponentModel.PropertyDescriptorCollection props = System.ComponentModel.TypeDescriptor.GetProperties(this); for (int i = 0; (i < props.Count); i = (i + 1)) { System.ComponentModel.PropertyDescriptor prop = props[i]; if ((prop.IsReadOnly != true)) { try { if ((dr[prop.Name].Equals(System.DBNull.Value) != true)) { if ((prop.PropertyType.Equals(dr[prop.Name].GetType()) != true)) { prop.SetValue(this, prop.Converter.ConvertFrom(dr[prop.Name])); } else { prop.SetValue(this, dr[prop.Name]); } } } catch (System.Exception) { } } } }
/// <summary> /// Vincula o valor da propriedade. /// </summary> /// <param name="controllerContext"></param> /// <param name="bindingContext"></param> /// <param name="propertyDescriptor"></param> protected override void BindProperty(System.Web.Mvc.ControllerContext controllerContext, System.Web.Mvc.ModelBindingContext bindingContext, System.ComponentModel.PropertyDescriptor propertyDescriptor) { string prefix = CreateSubPropertyName(bindingContext.ModelName, propertyDescriptor.Name); var clearProperties = controllerContext.Controller.ViewData.ContainsKey(EntityModelMetadataProvider.ClearPropertyNamesKey) ? controllerContext.Controller.ViewData[EntityModelMetadataProvider.ClearPropertyNamesKey] as IEnumerable <string> : null; var propertyPath = prefix; if (clearProperties != null) { var parts = prefix.Split('.'); if (parts.Length > 1 && parts[parts.Length - 2].EndsWith("]")) { var index = 0; propertyPath = string.Join(".", parts.Skip(1).Select(f => (index = f.IndexOf('[')) >= 0 ? f.Substring(0, index) : f)); } else if (parts.Length > 1) { propertyPath = string.Join(".", parts.Skip(1)); } } if (!bindingContext.ValueProvider.ContainsPrefix(prefix) && clearProperties != null && clearProperties.Contains(propertyPath)) { var propertyValue = propertyDescriptor.GetValue(bindingContext.Model) as System.Collections.IList; if (propertyValue != null) { propertyValue.Clear(); } else if (!propertyDescriptor.IsReadOnly) { propertyDescriptor.SetValue(bindingContext.Model, null); } } base.BindProperty(controllerContext, bindingContext, propertyDescriptor); }
protected override void SetProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, System.ComponentModel.PropertyDescriptor propertyDescriptor, object value) { HttpRequestBase request = controllerContext.HttpContext.Request; // Detect if this is a JSON request if (request.IsAjaxRequest() && request.ContentType.StartsWith("application/json", StringComparison.OrdinalIgnoreCase)) { // See if the value is a DateTime if (value is DateTime) { // This is double casting, but since it's a value type there's not much other things we can do DateTime dateValue = (DateTime)value; if (dateValue.Kind == DateTimeKind.Local) { // Change it DateTime utcDate = dateValue.ToUniversalTime(); if (!propertyDescriptor.IsReadOnly && propertyDescriptor.PropertyType == typeof(DateTime)) { propertyDescriptor.SetValue(bindingContext.Model, utcDate); } return; } } } // Fall back to the default behavior base.SetProperty(controllerContext, bindingContext, propertyDescriptor, value); }
protected override void SetProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, System.ComponentModel.PropertyDescriptor propertyDescriptor, object value) { //special case for DateTime if (propertyDescriptor.PropertyType == typeof(DateTime)) { if (propertyDescriptor.IsReadOnly) { return; } try { if (value != null) { DateTime dt = (DateTime)value; propertyDescriptor.SetValue(bindingContext.Model, dt.ToUniversalTime()); } } catch (Exception ex) { string modelStateKey = CreateSubPropertyName(bindingContext.ModelName, propertyDescriptor.Name); bindingContext.ModelState.AddModelError(modelStateKey, ex); } } else { //handles all other types base.SetProperty(controllerContext, bindingContext, propertyDescriptor, value); } }
public virtual void SetEditValue(System.ComponentModel.PropertyDescriptor property, object value) { if (mEditingRow == null) { throw new DevAgeApplicationException("There isn't a row in editing state, call BeginAddNew or BeginEdit first"); } property.SetValue(mEditingRow, value); }
/// <summary> /// Sets the value of the specified component to the specified value, /// if it has a matching descriptor</summary> /// <param name="component">Component for which to set the value</param> /// <param name="value">New value, must be compatible with value type of the descriptor</param> public override void SetValue(object component, object value) { SysPropertyDescriptor descriptor = FindDescriptor(component); if (descriptor != null) { descriptor.SetValue(component, value); } }
/*int z = 0; * string sz = "";*/ protected override void BindProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, System.ComponentModel.PropertyDescriptor propertyDescriptor) { base.BindProperty(controllerContext, bindingContext, propertyDescriptor); /* * sz += ", " + propertyDescriptor.Name; ++z; * * if (z == 9) * throw new Exception(sz);*/ /*if (propertyDescriptor.Name == "SelectField") * throw new Exception(controllerContext.HttpContext.Request["select_field"]);*/ /*if (propertyDescriptor.Name == "ShowFieldRaw") * throw new Exception(controllerContext.HttpContext.Request["show_field"]);*/ foreach (var p in propertyDescriptor.Attributes) { if (p.GetType() == typeof(BindingNameAttribute)) { var b = p as BindingNameAttribute; object value = controllerContext.HttpContext.Request[b.Name]; if (propertyDescriptor.PropertyType == typeof(int)) { propertyDescriptor.SetValue(bindingContext.Model, Convert.ToInt32(value)); } else { propertyDescriptor.SetValue(bindingContext.Model, value); } break; } } //foreach }
protected override void BindProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, System.ComponentModel.PropertyDescriptor propertyDescriptor) { base.BindProperty(controllerContext, bindingContext, propertyDescriptor); for (int i = 0; i < propertyDescriptor.Attributes.Count; i++) { if (propertyDescriptor.Attributes[i].GetType() == typeof(BindingNameAttribute)) { // set property value. propertyDescriptor.SetValue(bindingContext.Model, controllerContext.HttpContext.Request.Form[(propertyDescriptor.Attributes[i] as BindingNameAttribute).Name]); break; } } }
protected override void BindProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, System.ComponentModel.PropertyDescriptor propertyDescriptor) { PropertyInfo pInfo = bindingContext.Model.GetType().GetProperty("ID"); if (pInfo != null) { int id = (int)pInfo.GetValue(bindingContext.Model, null); switch (propertyDescriptor.Name) { case "Created_By": if (id == 0) { propertyDescriptor.SetValue(bindingContext.Model, controllerContext.HttpContext.User.Identity.Name); } break; case "Created_Date": if (id == 0) { propertyDescriptor.SetValue(bindingContext.Model, DateTime.Now); } break; case "Updated_By": propertyDescriptor.SetValue(bindingContext.Model, controllerContext.HttpContext.User.Identity.Name); break; case "Last_Updated_Date": propertyDescriptor.SetValue(bindingContext.Model, DateTime.Now); break; case "Active_Ind": if (id == 0) { propertyDescriptor.SetValue(bindingContext.Model, true); } break; case "Site_Org_ID": propertyDescriptor.SetValue(bindingContext.Model, Convert.ToInt32(controllerContext.HttpContext.Session["Site_Org_ID"])); break; case "Unique_ID": if (id == 0) { propertyDescriptor.SetValue(bindingContext.Model, Convert.ToInt32(controllerContext.HttpContext.Session["Unique_ID"])); } break; } } base.BindProperty(controllerContext, bindingContext, propertyDescriptor); }
protected override void BindProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, System.ComponentModel.PropertyDescriptor propertyDescriptor) { if (propertyDescriptor.Name == "TypeOfBoard") { TypeOfBoard tob = (TypeOfBoard)Enum.Parse(typeof(TypeOfBoard), controllerContext.HttpContext.Request.Form["type"]); propertyDescriptor.SetValue(bindingContext.Model, tob); return; } if (propertyDescriptor.Name == "Result") { var request = controllerContext.HttpContext.Request.Form["result"]; if (request != null) { TypeOfResult tor = (TypeOfResult)Enum.Parse(typeof(TypeOfResult), request); propertyDescriptor.SetValue(bindingContext.Model, tor); return; } } base.BindProperty(controllerContext, bindingContext, propertyDescriptor); }
protected override void BindProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, System.ComponentModel.PropertyDescriptor propertyDescriptor) { NameValueCollection values = controllerContext.HttpContext.Request.Form; if (propertyDescriptor.PropertyType.Equals(typeof(ShortStrOra))) { ShortStrOra value = new ShortStrOra(values[propertyDescriptor.Name]); propertyDescriptor.SetValue(bindingContext.Model, value); return; } else { base.BindProperty(controllerContext, bindingContext, propertyDescriptor); } }
//protected override object CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, 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); // } // else if (modelType.IsAbstract) // { // string concreteTypeName = bindingContext.ModelName + ".Type"; // var concreteTypeResult = bindingContext.ValueProvider.GetValue(concreteTypeName); // if (concreteTypeResult == null) // throw new Exception("Concrete type for abstract class not specified"); // type = Assembly.GetExecutingAssembly().GetTypes().SingleOrDefault(t => t.IsSubclassOf(modelType) && t.Name == concreteTypeResult.AttemptedValue); // if (type == null) // throw new Exception(String.Format("Concrete model type {0} not found", concreteTypeResult.AttemptedValue)); // var instance = Activator.CreateInstance(type); // bindingContext.ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(() => instance, type); // return instance; // } // else // { // return Activator.CreateInstance(modelType); // } //} //public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) //{ // var dictionaryBindingContext = new ModelBindingContext() // { // ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(() => null, typeof(IDictionary<object,object>)), // ModelName = "FileName,Description,Owner,CreationDate,checkLock", //The name(s) of the form elements you want going into the dictionary // ModelState = bindingContext.ModelState, // PropertyFilter = bindingContext.PropertyFilter, // ValueProvider = bindingContext.ValueProvider // }; // var model = base.BindModel(controllerContext, dictionaryBindingContext); // //var model = base.BindModel(controllerContext, bindingContext); // return model; //} //public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) //{ // var model = base.BindModel(controllerContext, bindingContext) as Files; //Bind most of the model using the built-in binder, this will bind all primitives for us // model.files. // const string magicString = "files"; //TODO Use of magic strings // var files = bindingContext.ValueProvider.GetValue(magicString); //Get the posted value for files // if (files != null && !string.IsNullOrEmpty(files.AttemptedValue)) // { //Check we have a value for files before we proceed // bindingContext.ModelState.Remove(magicString); //Remove binding conversion errors for files, as we are going to deal with binding files ourselves // try // { // //Create a list of files based on the comma delimited string of posted OfficeId's // model.files = new List( // files.AttemptedValue.Split(",".ToCharArray()) // .Select(id => new FileModel() { }) // .ToList() // ); // } // catch (FormatException ex) // { //Catch if the posted files are not posted as a comma delimited string of Guids // bindingContext.ModelState.AddModelError(magicString, ex); //Add an error to the model state, used for ModelState.IsValid and Html error helpers // } // catch (Exception ex) // { //Unexpected exception // throw ex; // } // } // return model; //} protected override void BindProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, System.ComponentModel.PropertyDescriptor propertyDescriptor) { //if (propertyDescriptor.PropertyType == typeof(List<FileModel>)) //{ // var fileList = new List<FileModel>(); // var form = controllerContext.HttpContext.Request.Form; // var keys = form.Keys; // foreach (var key in keys) // { // } //} //{ // var incomingData = bindingContext.ValueProvider.GetValue("Edit." + propertyDescriptor.Name + "[]"); // if (incomingData != null) // { // List<FileModel> fileModels = new List<FileModel>(); // fileModels = (List<FileModel>)incomingData.ConvertTo(typeof(List<FileModel>)); // var model = bindingContext.Model as Files; // model.files = fileModels; // } //} if (propertyDescriptor.Name == "Archive") { var list = new List <int>(5); var form = controllerContext.HttpContext.Request.Form; var ids = form.AllKeys.Where(x => x.StartsWith("id")); foreach (var id in ids) { int i; if (int.TryParse(form.Get(id), out i)) { list.Add(i); } } SetProperty(controllerContext, bindingContext, propertyDescriptor, list); } if (propertyDescriptor.Name == "Search") { propertyDescriptor.SetValue(bindingContext.Model, controllerContext.HttpContext.Request.Form["filename"]); } base.BindProperty(controllerContext, bindingContext, propertyDescriptor); }
protected override void BindProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, System.ComponentModel.PropertyDescriptor propertyDescriptor) { if (propertyDescriptor.PropertyType == typeof(TagList)) { ValueProviderResult value = bindingContext.ValueProvider.GetValue(propertyDescriptor.Name); string[] rawTags = value.ToString().Split(','); TagList tags = new TagList(); foreach (string rawTag in rawTags) { // for numbers - get them from DB // for strings - create new and store in DB // then add them to tags } propertyDescriptor.SetValue(bindingContext.Model, tags); } else { base.BindProperty(controllerContext, bindingContext, propertyDescriptor) } }
protected override void BindProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, System.ComponentModel.PropertyDescriptor propertyDescriptor) { if (propertyDescriptor.Name == "StartDate") { var date = controllerContext.HttpContext.Request.Form["StartDate"]; DateTime dt = DateTime.Parse(date); //int.Parse(controllerContext.HttpContext.Request.Form["month"]), //int.Parse(controllerContext.HttpContext.Request.Form["day"])); propertyDescriptor.SetValue(bindingContext.Model, dt.AddYears(5)); return; } base.BindProperty(controllerContext, bindingContext, propertyDescriptor); //ValueProviderResult result = bindingContext.ValueProvider.GetValue(bindingContext.ModelName); //base.BindProperty(controllerContext, bindingContext, propertyDescriptor); }
//public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) //{ // var valueResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName); // if (valueResult == null) // return null; // var modelState = new ModelState { Value = valueResult }; // object actualValue = null; // try // { // var parts = valueResult.AttemptedValue.Split('/'); //ex. 1391/1/19 // if (parts.Length != 3) return null; // int year = int.Parse(parts[0]); // int month = int.Parse(parts[1]); // int day = int.Parse(parts[2]); // actualValue = new DateTime(year, month, day, new PersianCalendar()); // } // catch (FormatException e) // { // modelState.Errors.Add(e); // } // bindingContext.ModelState.Add(bindingContext.ModelName, modelState); // return actualValue; //} protected override void BindProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, System.ComponentModel.PropertyDescriptor propertyDescriptor) { if (propertyDescriptor.PropertyType == typeof(DateTime) || propertyDescriptor.PropertyType == typeof(Nullable <DateTime>)) { var val = propertyDescriptor.GetValue(bindingContext.Model); if (val != null) { var parts = val.ToString().Split('/'); if (parts.Length == 3) { int year = int.Parse(parts[0]); int month = int.Parse(parts[1]); int day = int.Parse(parts[2]); var actualValue = new DateTime(year, month, day, new PersianCalendar()); propertyDescriptor.SetValue(bindingContext.Model, actualValue); } } } base.BindProperty(controllerContext, bindingContext, propertyDescriptor); // bind the other properties here }
protected override void BindProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, System.ComponentModel.PropertyDescriptor propertyDescriptor) { if (propertyDescriptor.Name == "Bundle") { string value = controllerContext.HttpContext.Request.Form["input-tags"]; string[] split = value.Split(new char[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries); List <Bundle> bundleList = new List <Bundle>(); foreach (var str in split) { int Id; if (int.TryParse(str, out Id)) { Bundle bundle = new Bundle(); bundle.TagId = Id; bundleList.Add(bundle); } else { string s = str.Substring(1); string title = s.Trim(); if (!String.IsNullOrEmpty(title)) { Tag tag = new Tag(); tag.Title = title; Bundle bundle = new Bundle(); bundle.Tag = tag; bundleList.Add(bundle); } } } propertyDescriptor.SetValue(bindingContext.Model, bundleList); } base.BindProperty(controllerContext, bindingContext, propertyDescriptor); }
protected override void SetProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, System.ComponentModel.PropertyDescriptor propertyDescriptor, object value) { if (propertyDescriptor.PropertyType == typeof(Choose)) { var valueKey = string.IsNullOrEmpty(bindingContext.ModelName) ? propertyDescriptor.Name : string.Format("{0}.{1}", bindingContext.ModelName, propertyDescriptor.Name); valueKey += ".SelectedValue"; var listItemValue = bindingContext.ValueProvider.GetValue(valueKey).AttemptedValue; var items = propertyDescriptor.GetValue(bindingContext.Model) as Choose; if (items == null) { items = new Choose(); propertyDescriptor.SetValue(bindingContext.Model, items); } items.SelectedValue = listItemValue; return; } base.SetProperty(controllerContext, bindingContext, propertyDescriptor, value); }
void ctrol_EulerChanged(object arg1, Euler arg2) { pd.SetValue(component, arg2.ToMatrix()); }