溢出和比拉尔,感谢您回答我的问题。
@Bilal:我对保存和提交使用相同的模型,并且不希望模型上有任何属性,而是需要控制器/动作级别的东西。
在寻找更好的答案时,我想出了这样的东西。我从另一篇文章中读到了这篇文章,但失去了链接。一旦我得到它,我会更新相同的。
添加新的操作过滤器属性
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public class IgnoreValidationAttribute : FilterAttribute, IAuthorizationFilter
{
// TODO: Try to put it on another more appropriate method such as OnActionExcecuting.
// Looks like - This is the earliest method we can interpret before an action. I really dont like this!
public void OnAuthorization(AuthorizationContext filterContext)
{
//TODO: filterContext != null && filterContext.httpContext != null
var itemKey = this.CreateKey(filterContext.ActionDescriptor);
if (!filterContext.HttpContext.Items.Contains(itemKey))
{
filterContext.HttpContext.Items.Add(itemKey, true);
}
}
private string CreateKey(ActionDescriptor actionDescriptor)
{
var action = actionDescriptor.ActionName.ToLower();
var controller = actionDescriptor.ControllerDescriptor.ControllerName.ToLower();
return string.Format("IgnoreValidation_{0}_{1}", controller, action);
}
}
覆盖 DataAnnotationModelMetadata
public class IgnoreValidationModelMetaData : DataAnnotationsModelMetadata
{
public IgnoreValidationModelMetaData(DataAnnotationsModelMetadataProvider provider, Type containerType,
Func<object> modelAccessor, Type modelType, string propertyName,
DisplayColumnAttribute displayColumnAttribute) :
base(provider, containerType, modelAccessor, modelType, propertyName, displayColumnAttribute)
{
}
public override IEnumerable<ModelValidator> GetValidators(ControllerContext context)
{
var itemKey = this.CreateKey(context.RouteData);
if (context.HttpContext.Items[itemKey] != null && bool.Parse(context.HttpContext.Items[itemKey].ToString()) == true)
{
return Enumerable.Empty<ModelValidator>();
}
return base.GetValidators(context);
}
private string CreateKey(RouteData routeData)
{
var action = (routeData.Values["action"] ?? null).ToString().ToLower();
var controller = (routeData.Values["controller"] ?? null).ToString().ToLower();
return string.Format("IgnoreValidation_{0}_{1}", controller, action);
}
}
现在告诉提供者使用我们的自定义数据注释元数据并在操作方法中存在 IgnoreValidationAttribute 时清空验证
public class IgnoreValidationModelMetaDataProvider : DataAnnotationsModelMetadataProvider
{
protected override ModelMetadata CreateMetadata(IEnumerable<Attribute> attributes,
Type containerType, Func<object> modelAccessor, Type modelType, string propertyName)
{
var displayColumnAttribute = new List<Attribute>(attributes).OfType<DisplayColumnAttribute>().FirstOrDefault();
var baseMetaData = base.CreateMetadata(attributes, containerType, modelAccessor, modelType, propertyName);
// is there any other good strategy to copy the properties?
return new IgnoreValidationModelMetaData(this, containerType, modelAccessor, modelType, propertyName, displayColumnAttribute)
{
TemplateHint = baseMetaData.TemplateHint,
HideSurroundingHtml = baseMetaData.HideSurroundingHtml,
DataTypeName = baseMetaData.DataTypeName,
IsReadOnly = baseMetaData.IsReadOnly,
NullDisplayText = baseMetaData.NullDisplayText,
DisplayFormatString = baseMetaData.DisplayFormatString,
ConvertEmptyStringToNull = baseMetaData.ConvertEmptyStringToNull,
EditFormatString = baseMetaData.EditFormatString,
ShowForDisplay = baseMetaData.ShowForDisplay,
ShowForEdit = baseMetaData.ShowForEdit,
Description = baseMetaData.Description,
ShortDisplayName = baseMetaData.ShortDisplayName,
Watermark = baseMetaData.Watermark,
Order = baseMetaData.Order,
DisplayName = baseMetaData.DisplayName,
IsRequired = baseMetaData.IsRequired
};
}
}
用法
[HttpPost]
[IgnoreValidation]
public ActionResult SaveDraft(MyModel myModel)
{
if (ModelState.IsValid)
{
// Should always reach here
}
.......
}
[HttpPost]
public ActionResult Submit(MyModel myModel)
{
if (ModelState.IsValid)
{
}
}
请不要忘记在您的 Application_Start 中为连接 'ModelMetadataProviders.Current = new IgnoreValidationModelMetaDataProvider(); 调用它。
不过有几个问题。
有没有比 OnAuthorization() 更早的地方我们可以操纵 HttpContext ?我不喜欢重写它来做与授权无关的事情的想法。请注意 OnActionExecuting() 在 MVC 管道中为时已晚,无法执行此操作(我尝试过此操作但无法正常工作)。
有没有比向 HttpContext 添加密钥并稍后使用它更好的方法呢?