Ich verwende Datenannotationen für clientseitige Validierungen. Ich habe zwei Szenarien, in denen ich [Required]
und [RequiredIfTrue]
verwende. Problem ist, dass meine bedingten Validierungen nicht funktionieren.Bedingte Validierungen funktionieren nicht
Arbeits:
Modell:
[DisplayName(@"Custom Email Confirmation Address")]
[EmailAddress(ErrorMessage = @"Invalid Email Address")]
public string CustomEmailConfirmationAddress { get; set; }
Ausblick:
<div>
<%=Html.RequiredLabelFor(m => m.CustomEmailConfirmationAddress) %>
<%=Html.TextBoxFor(m => m.CustomEmailConfirmationAddress, new { maxlength = 100 })%>
<%=Html.ValidationMessageFor(m => m.CustomEmailConfirmationAddress)%>
</div>
nicht funktioniert:
Szenario = Wenn ShowConceptOptInMessage
checke ist d als make ConceptEmailOptInMessage
und PrivacyPolicy
Feld erforderlich.
Modell:
[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; }
Ausblick:
<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>
Controller-Methode für beide Szenario:
Controller:
[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);
}
Nicht sicher, warum Sie eine Antwort akzeptiert haben, die nichts mit Ihrem Problem zu tun hat und ist nur ein schrecklicher Hack. Angenommen, Sie verwenden [idiotensicher] (http://foolproof.codeplex.com/) '[RequiredIfTrue]' und Sie haben die relevanten Skripts eingefügt, dann funktioniert Ihr Code einwandfrei. –