public virtual ActionResult Update(Guid id) { TEntity entity; if (id == Guid.Empty) { if (!EntityQueryable.Addable()) { return(new HttpStatusCodeResult(403)); } if (!Metadata.AddRoles.All(t => User.IsInRole(t))) { return(new HttpStatusCodeResult(403)); } entity = EntityQueryable.Create(); } else { if (!EntityQueryable.Editable()) { return(new HttpStatusCodeResult(403)); } if (!Metadata.EditRoles.All(t => User.IsInRole(t))) { return(new HttpStatusCodeResult(403)); } entity = EntityQueryable.GetEntity(id); if (entity == null) { return(new HttpStatusCodeResult(404)); } } var properties = Metadata.Properties.Where(t => !t.IsHiddenOnEdit).ToArray(); for (int i = 0; i < properties.Length; i++) { PropertyMetadata propertyMetadata = properties[i]; if (propertyMetadata.Type == ComponentModel.DataAnnotations.CustomDataType.File || propertyMetadata.Type == ComponentModel.DataAnnotations.CustomDataType.Image) { #region File Path Value if (!Request.Files.AllKeys.Contains(propertyMetadata.Property.Name)) { continue; } if (!(this is IFileController <TEntity>)) { throw new NotSupportedException("Controller doesn't support upload file."); } ((IFileController <TEntity>) this).SaveFileToProperty(entity, propertyMetadata, Request.Files[propertyMetadata.Property.Name]); #endregion } else { #region Property Value if (!Request.Form.AllKeys.Contains(propertyMetadata.Property.Name)) { if (id == Guid.Empty && propertyMetadata.IsRequired) { Response.StatusCode = 400; return(Content(propertyMetadata.Name + "为必填项")); } continue; } string originValue = Request.Form[propertyMetadata.Property.Name]; if (string.IsNullOrEmpty(originValue) && propertyMetadata.Property.PropertyType != typeof(string) && propertyMetadata.IsRequired) { Response.StatusCode = 400; return(Content(propertyMetadata.Name + "为必填项")); } //Type type = propertyMetadata.Property.PropertyType; //if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>)) // type = type.GetGenericArguments()[0]; TypeConverter converter = EntityValueConverter.GetConverter(propertyMetadata); if (converter == null) { if (propertyMetadata.Property.PropertyType.IsGenericType && propertyMetadata.Property.PropertyType.GetGenericTypeDefinition() == typeof(ICollection <>)) { converter = new Converter.CollectionConverter(); } else if (propertyMetadata.Property.PropertyType.IsEnum) { converter = new EnumConverter(propertyMetadata.Property.PropertyType); } else if (propertyMetadata.Type != ComponentModel.DataAnnotations.CustomDataType.Password) { throw new NotSupportedException("Type of \"" + propertyMetadata.Property.PropertyType.Name + "\" converter not found."); } } if (propertyMetadata.Type == ComponentModel.DataAnnotations.CustomDataType.Password && entity is IPassword) { object v = propertyMetadata.Property.GetValue(entity); if (v == null || originValue != v.ToString()) { ((IPassword)entity).SetPassword(originValue); } } else { EntityValueConverterContext context = new EntityValueConverterContext(EntityBuilder.DescriptorContext, propertyMetadata); object value = converter.ConvertFrom(context, null, originValue); if (converter.GetType() == typeof(Converter.CollectionConverter)) { object collection = propertyMetadata.Property.GetValue(entity); ((dynamic)collection).Clear(); var addMethod = collection.GetType().GetMethod("Add"); object[] array = (object[])value; for (int a = 0; a < array.Length; a++) { addMethod.Invoke(collection, new object[] { array[a] }); } } else { propertyMetadata.Property.SetValue(entity, value); } } #endregion } } var validateResult = entity.Validate(null); if (validateResult.Count() != 0) { Response.StatusCode = 400; return(Content(new string(validateResult.SelectMany(t => t.ErrorMessage += "/r/n").ToArray()))); } bool result; if (id == Guid.Empty) { result = EntityQueryable.Add(entity); } else { result = EntityQueryable.Edit(entity); } if (result) { return(Content(entity.Index.ToString())); } else { Response.StatusCode = 400; return(Content("未知")); } }