简体   繁体   English

MVC3 jQuery-部分验证

[英]MVC3 jQuery - partial validation

I have a form in which a user enters 2 types of data: 1. Project details 2. Survey questions related to the project 我有一个表单,用户可以在其中输入2种类型的数据:1.项目详细信息2.调查与项目相关的问题

The form has 2 submit buttons 1. Save Draft 2. Submit 该表单有2个提交按钮1.保存草稿2.提交

The 'Save Draft' post needs to validate only project details and not survey fields. “保存草稿”帖子仅需要验证项目详细信息,而无需验证调查字段。 The Submit post should validate both project and survey data. 提交帖子应验证项目和调查数据。

Is there a way to partially validate data when user clicks 'Save Draft'? 当用户单击“保存草稿”时,是否可以部分验证数据? Maybe handle the 'Save Draft' click and ignore/remove validation for the survey fields... 也许处理“保存草稿”点击并忽略/删除调查字段的验证...

You can do partial validation by with an action filter attribute. 您可以使用动作过滤器属性进行部分验证。 You might be able to customize to your needs. 您也许可以根据需要进行自定义。 It gives you access to the request and the model state. 它使您可以访问请求和模型状态。 With access to the model state you can modify the validation errors. 通过访问模型状态,您可以修改验证错误。

public class ValidateDraftAttribute : ActionFilterAttribute 
{  
  public override void OnActionExecuting(ActionExecutingContext filterContext)
  {

     var modelState = filterContext.Controller.ViewData.ModelState;
     var incomingValues = filterContext.Controller.ValueProvider;

      modelState[key].Errors.Clear();

  }
}

The attribute is then added to the controller. 然后将该属性添加到控制器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM