2

我的控制器中有以下操作:

[HttpGet]
public ActionResult Office(GestionOffice model)
{
    ModelState.Clear();
    model.Initialize();
    return View(model);
}

我不需要通过 GET 采取行动,进行验证。这将帮助我执行 GET 调用。

这将是我的理想情况:

[HttpGet]
[NotValidateModel]
public ActionResult Office(GestionOffice model)
{
    model.Initialize();
    return View(model);
}

谢谢你。

编辑

NotValidateModel澄清不存在,将归因于案例以避免验证。

将模型移动到行动中的原因是MOCK模型

编辑二

我有我的 POST 操作,我需要通过 GET 接收我的操作,没有验证的模型,以成功完成控制器中操作的测试

[HttpGet]
[NotValidateModel]
public ActionResult Office(GestionOffice model)
{
    model.Initialize();
    return View(model);
}

[HttpPost]
[ActionName("Office")]
[NotValidateModel]
public ActionResult OfficePost(GestionOffice model)
{
    if(ModelState.IsValid)
    {
        model.Save();
        return RedirectToAction("List");
    }

    model.Initialize();
    return View(model);
}

关于@Mark 解决方案的版本

在我看来,我有一些动作调用,我必须使用动作和控制器创建一个键。

自定义模型元数据

public class CustomModelMetaData : ModelMetadata
{
    public CustomModelMetaData(ModelMetadataProvider provider, Type containerType, Func<object> modelAccessor, Type modelType, string propertyName)
        : base(provider, containerType, modelAccessor, modelType, propertyName)
    {
    }

    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("NoValidation_{0}_{1}", controller, action);
    }
}

筛选

public class NoValidationAttribute : FilterAttribute, IAuthorizationFilter
{
    public void OnAuthorization(AuthorizationContext filterContext)
    {
        var itemKey = this.CreateKey(filterContext.ActionDescriptor);
        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("NoValidation_{0}_{1}", controller, action);
    }
}

编辑过滤器

可能存在在主视图中有一个 foreach 以调用具有属性的几个局部视图的情况NoValidation。在这种情况下,包括一个检查密钥的控件。因为它在key中包含了controller的名字和action,这个key几乎是唯一的,只能在描述的情况下重复

public class NoValidationAttribute : FilterAttribute, IAuthorizationFilter
{
    public void OnAuthorization(AuthorizationContext filterContext)
    {
        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("NoValidation_{0}_{1}", controller, action);
    }
}
4

4 回答 4

2

[请参阅下面的更新以及@ANDRES 在问题中提到的编辑]

您尝试做的事情很难实现,我不确定您为什么要这样做。我试图通过自定义ModelMetaData和过滤来完成(未完全测试)。

我们无法从模型绑定器或元数据提供程序中读取通过操作修饰的属性,因此我们需要一个过滤器。过滤器在 中添加了一些值,HttpContext.Items我们可以从自定义元数据提供程序(http://stackoverflow.com/questions/6198155/asp-net-mvc-modelbinder-getting-action-method)中检索它。

筛选

public class NoValidationAttribute : FilterAttribute, IAuthorizationFilter
{
    public void OnAuthorization(AuthorizationContext filterContext)
    {
      // please see the edits in question to see the key generation
      filterContext.HttpContext.Items.Add("NoValidation", true);
    }
}

自定义模型元数据

public class CustomModelMetaData : ModelMetadata
{
    public CustomModelMetaData(ModelMetadataProvider provider, Type containerType, Func<object> modelAccessor, Type modelType, string propertyName) :
      base(provider, containerType, modelAccessor, modelType, propertyName)
    {
    }

    public override System.Collections.Generic.IEnumerable<ModelValidator> GetValidators(ControllerContext context)
    {
      if (context.HttpContext.Items["NoValidation"] != null && bool.Parse(context.HttpContext.Items["NoValidation"].ToString()) == true)
        return Enumerable.Empty<ModelValidator>();

      return base.GetValidators(context);
    }
}

自定义 ModelMetaDataProvider

public class CustomModelMetaDataProvider : DataAnnotationsModelMetadataProvider
{
    protected override ModelMetadata CreateMetadata(System.Collections.Generic.IEnumerable<Attribute> attributes,
      Type containerType, Func<object> modelAccessor,
      Type modelType,
      string propertyName)
    {
      return new CustomModelMetaData(this, containerType, modelAccessor, modelType, 
         propertyName); 
    }
}

全球.asax.cs

ModelMetadataProviders.Current = new CustomModelMetaDataProvider();

用法

[HttpGet]
[NoValidation]
public ActionResult Office(GestionOffice model)
{
   ...
}

==================================更新================ ======================

CustomModelMetaData而不是通过继承来创建 aModelMetadata我认为继承自DataAnnotationsModelMetadata.

public class CustomModelMetaData : DataAnnotationsModelMetadata
{
    public CustomModelMetaData(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("NoValidation_{0}_{1}", controller, action);
    }
}

CustomModelMetaDataProvider你必须设置一些属性。

public class CustomModelMetaDataProvider : 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 CustomModelMetaData(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
      };
    }
}
于 2012-07-06T20:52:08.343 回答
1

是的,您使用这样的[ValidateInput(false)]属性:

[ValidateInput(false)]
[HttpGet]
public ActionResult Office(GestionOffice model)
{
    model.Initialize();
    return View(model);
}
于 2012-07-06T18:40:56.497 回答
0

验证发生在 POST 中,因此您必须删除Model.IsValid并添加[ValidateInput(false)]

[HttpPost]
[ValidateInput(false)]
[ActionName("Office")]
public ActionResult OfficePost(GestionOffice model)
{        
    model.Save();
    return RedirectToAction("List");
}

现在帖子将成功,但如果您插入数据库,则必须小心。

于 2012-07-06T19:51:50.927 回答
0

如果您想在生产代码中使用而不进行验证,则需要禁用客户端验证,因为在服务器端它将被禁用。

将 web.config 修改为休闲:

<appSettings>
    <add key="ClientValidationEnabled" value="false" />
</appSettings>
于 2012-07-06T19:30:19.517 回答