【问题标题】:Conditional validations are not working条件验证不起作用
【发布时间】:2016-04-11 14:18:38
【问题描述】:

我正在使用数据注释进行客户端验证。我有两种使用[Required][RequiredIfTrue] 的场景。问题是我的条件验证不起作用。

工作:

型号:

[DisplayName(@"Custom Email Confirmation Address")]
[EmailAddress(ErrorMessage = @"Invalid Email Address")]
public string CustomEmailConfirmationAddress { get; set; }

查看:

<div>
    <%=Html.RequiredLabelFor(m => m.CustomEmailConfirmationAddress) %>
    <%=Html.TextBoxFor(m => m.CustomEmailConfirmationAddress, new { maxlength = 100 })%>
    <%=Html.ValidationMessageFor(m => m.CustomEmailConfirmationAddress)%>
</div>

不工作:

场景 = 如果选中 ShowConceptOptInMessage 而不是使 ConceptEmailOptInMessagePrivacyPolicy 字段成为必需。

型号:

[DisplayName("Show Concept Opt-In Message")]
public bool ShowConceptOptInMessage { get; set; }

[RequiredIfTrue("ShowConceptOptInMessage", ErrorMessage = "Concept Email Opt-In Message is required")]
[DisplayName("Concept Email Opt-In Message")]
public string ConceptEmailOptInMessage { get; set; }

[RequiredIfTrue("ShowConceptOptInMessage", ErrorMessage = "Concept Privacy Policy is required")]
[DisplayName("Concept Privacy Policy")]
public string PrivacyPolicy { get; set; }

查看:

<div>
    <%=Html.LabelFor(m => m.ShowConceptOptInMessage) %>
    <%=Html.CheckBoxFor(m => m.ShowConceptOptInMessage)%>
    <%=Html.ValidationMessageFor(m => m.ShowConceptOptInMessage)%>
</div>

 <div>
    <%=Html.LabelFor(m => m.ConceptEmailOptInMessage) %>
    <%=Html.TextAreaFor(m => m.ConceptEmailOptInMessage, new { maxlength = 1000 })%>
    <%= Html.ValidationMessageFor(m => m.ConceptEmailOptInMessage)%>
</div>

<div>
    <%=Html.LabelFor(m => m.PrivacyPolicy) %>
    <%=Html.TextAreaFor(m => m.PrivacyPolicy)%>
    <%= Html.ValidationMessageFor(m => m.PrivacyPolicy)%>
</div>

两种场景的控制器方法:

控制器:

[HttpPost]
public ActionResult Edit(ConceptConfigurationModel model)
{
    try
    {
        if (this.ModelState.IsValid)
        {
            // model
            this.ConceptManager.SaveConcept(model);
            model.Submitted = true;
        }
    }
    catch (BusinessLogicException ex)
    {
        this.ModelState.AddModelError("ConceptName", ex.Message);
    }
    ModelState.Clear();
    this.ConceptManager.FillConceptModel(model);

    return View(model);
}

【问题讨论】:

  • 不知道为什么你接受了一个与你的问题无关的答案,这只是一个可怕的黑客攻击。假设您使用 foolproof [RequiredIfTrue] 并且您已包含相关脚本,那么您的代码可以正常工作。

标签: c# asp.net-mvc-4 data-annotations


【解决方案1】:

使用 ASP.NET MVC 4,RequiredIf 不起作用,我使用 jquery unobtrusive 实现了同样的效果,添加以下 javascript 方法

function AddValidation()
{
var showConceptOptInMessage = $("#ShowConceptOptInMessage").val();
  if(showConceptOptInMessage)
{
  $("#ConceptEmailOptInMessage").attr('data-val-required', 'Concept Email Opt-In Message is required');
  $("#PrivacyPolicy ").attr('data-val-required', 'Concept Privacy Policy is required');
 }
   $('form').removeData('validator');
    $('form').removeData('unobtrusiveValidation');
    $.validator.unobtrusive.parse('form');
}

【讨论】:

  • 我添加了这个,showConceptOptInMessage 的值是undefined。我在这里想念什么?如果我执行类似 ' m.SnapfingerOptInDefaultToChecked, new { onclick="AddValidation()"} )%>` 的操作,如何调用此函数或作为容器传递的内容。或者有没有其他方法可以在复选框点击事件上调用这个函数?
  • 我已经编辑了答案删除了容器,因为您没有作为主 div 名称的 div,直接调用方法中的 dom 元素,即 AddValidation() { $("# ShowConceptOptInMessage").val()}
  • 我想通了,但问题是,一旦我检查了它正在工作的字段,但是当我取消选中它时,应该删除这个验证,而这并没有发生。我试过 `$(" #ConceptEmailOptInMessage").attr('data-val-required', 'Concept Email Opt-In Message is required', true);` & ` $(" #ConceptEmailOptInMessage").attr('data -val-required', 'Concept Email Opt-In Message is required', false);` 这不起作用
  • 每次调用 addvalidation 方法时,如果选中该复选框,则添加验证,如果未选中则删除属性,即 $(" #ConceptEmailOptInMessage").removeAttr('data-val-required') 类似那;并再次调用它 $('form').removeData('validator'); $('form').removeData('unobtrusiveValidation'); $.validator.unobtrusive.parse('form');
  • 微小的变化,$( "#ShowConceptOptInMessage").val(); 无论是选中还是未选中,总是给我 true,所以我使用了 $('#ShowConceptOptInMessage').is(':checked'),这给了我 true...false 基于选中...unchecked。谢谢
猜你喜欢
  • 2018-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-30
  • 1970-01-01
  • 2016-05-06
  • 2016-01-19
  • 2016-07-21
相关资源
最近更新 更多