public ActionResult EditRole(RoleEditModel model) { Stream stream = null; HttpPostedFileBase roleImage = Request.Files["RoleImage"]; Role role = roleService.Get(model.RoleName); if (roleImage != null && !string.IsNullOrEmpty(roleImage.FileName)) { TenantLogoSettings tenantLogoSettings = TenantLogoSettings.GetRegisteredSettings(TenantTypeIds.Instance().Role()); if (!tenantLogoSettings.ValidateFileLength(roleImage.ContentLength)) { ViewData["StatusMessageData"] = new StatusMessageData(StatusMessageType.Error, string.Format("文件大小不允许超过{0}", Formatter.FormatFriendlyFileSize(tenantLogoSettings.MaxLogoLength * 1024))); return(View(model)); } LogoSettings logoSettings = DIContainer.Resolve <ILogoSettingsManager>().Get(); if (!logoSettings.ValidateFileExtensions(roleImage.FileName)) { ViewData["StatusMessageData"] = new StatusMessageData(StatusMessageType.Error, "不支持的文件类型,仅支持" + logoSettings.AllowedFileExtensions); return(View(model)); } stream = roleImage.InputStream; model.RoleImage = roleImage.FileName; } else //当取不到上传的图片文件名时RoleImage值保持不变 { model.RoleImage = role != null ? role.RoleImage : string.Empty; } if (model != null && !string.IsNullOrEmpty(model.RoleName)) { if (role != null) { role = model.AsRole(); roleService.Update(role, stream); } } return(RedirectToAction("ManageUsers")); }
/// <summary> /// /// </summary> /// <param name="controllerContext"></param> /// <param name="bindingContext"></param> /// <returns></returns> public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { var value = base.BindModel(controllerContext, bindingContext); if (value == null) { return(value); } if (controllerContext.RouteData.Values.ContainsKey(bindingContext.ModelName)) { return(value); } string[] tempArray = null; if (bindingContext.ModelType.FullName.Contains("System.String") && value is Array) { tempArray = (string[])value; } //内容过滤 if ((tempArray != null && tempArray.Length > 0) || value is string && !string.IsNullOrEmpty(value as string)) { if (controllerContext.Controller.ValueProvider.ContainsPrefix(bindingContext.ModelName) || bindingContext.ModelMetadata.ContainerType != null) { //处理敏感词 WordFilterStatus status = WordFilterStatus.Banned; if (tempArray != null && tempArray.Length > 0) { for (int i = 0; i < tempArray.Length; i++) { tempArray[i] = WordFilter.SensitiveWordFilter.Filter(tempArray[i], out status); if (status == WordFilterStatus.Banned) { bindingContext.ModelState.AddModelError("SensitiveWord", "内容未通过验证或包含非法词语!"); break; } } return(tempArray); } string tempValue = (value as string).Trim(); Type type = bindingContext.ModelMetadata.ContainerType; PropertyInfo propertyInfo = null; if (type != null) { propertyInfo = type.GetProperty(bindingContext.ModelName); } var noFilterWordAttribute = propertyInfo != null?Attribute.GetCustomAttribute(propertyInfo, typeof(NoFilterWordAttribute)) as NoFilterWordAttribute : null; if (noFilterWordAttribute == null) { tempValue = WordFilter.SensitiveWordFilter.Filter(tempValue, out status); if (status == WordFilterStatus.Banned) { bindingContext.ModelState.AddModelError("SensitiveWord", "内容未通过验证或包含非法词语!"); return(tempValue); } } if (propertyInfo != null) { var dataTypeAttribute = Attribute.GetCustomAttribute(propertyInfo, typeof(DataTypeAttribute)) as DataTypeAttribute; if (dataTypeAttribute != null) { if (dataTypeAttribute.DataType == DataType.MultilineText) { //处理多行纯文本 tempValue = Formatter.FormatMultiLinePlainTextForStorage(tempValue, false); } else if (dataTypeAttribute.DataType == DataType.Html) { //处理html标签 tempValue = HtmlUtility.CleanHtml(tempValue, TrustedHtmlLevel.HtmlEditor); } } } return(tempValue); } } return(value); }