İstemci tarafı doğrulamaları için veri ek açıklamaları kullanıyorum. [Required]
ve [RequiredIfTrue]
kullanıyorum iki senaryo var. Sorun benim şartlı doğrulamalarım çalışmıyor.Koşullu doğrulamalar çalışmıyor
Çalışma:
Modeli:
[DisplayName(@"Custom Email Confirmation Address")]
[EmailAddress(ErrorMessage = @"Invalid Email Address")]
public string CustomEmailConfirmationAddress { get; set; }
Görünüm:
<div>
<%=Html.RequiredLabelFor(m => m.CustomEmailConfirmationAddress) %>
<%=Html.TextBoxFor(m => m.CustomEmailConfirmationAddress, new { maxlength = 100 })%>
<%=Html.ValidationMessageFor(m => m.CustomEmailConfirmationAddress)%>
</div>
Çalışma DEĞİL:
Senaryo = ShowConceptOptInMessage
checke ise d ConceptEmailOptInMessage
ve PrivacyPolicy
alanlarını doldurmak için gereklidir.
Modeli:
[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; }
Görünüm:
<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>
hem senaryo için
Kontrolör yöntemi:
Denetleyici:
ASP.NET MVC 4 ile[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);
}
emin değilim. [Foolproof] (http://foolproof.codeplex.com/) '[RequiredIfTrue]' yi kullandığınızı varsayarak ilgili betikleri ekledikten sonra kodunuz iyi çalışıyor. –