मेरे पास एक पंजीकरण पृष्ठ है जो आपके दिए गए देश और राज्य के लिए पूछता है। यह सब ठीक है और जब आप एक ऐसे देश से हैं, जिसमें अमेरिका जैसे राज्य हैं, लेकिन जब आप ऐसे देश से हैं, जिसमें राज्य नहीं हैं, तो मैं राज्य क्षेत्र को खाली छोड़ना चाहता हूं और फिर भी आपको पंजीकरण और जमा करने की अनुमति देता हूं। पृष्ठ अभी भी।
देश के लिए मेरी डीबी तालिका में मेरे पास एक कॉलम सेट बूलियन सत्य है यदि उनके पास राज्य हैं और यदि वे नहीं करते हैं तो गलत है। इस तरह जब कोई राज्यों वाले देश का चयन करता है तो वह ड्रॉप डाउन मेनू में राज्यों को लोड करता है।
मेरे पास इस तरह का मॉडल स्थापित है।
[Required(ErrorMessage = "State is required.")]
public string StateId { get; set; }
मैंने मूल्य को निरर्थक बनाने की कोशिश की, लेकिन इसका कोई असर नहीं हुआ। तो क्या DataAnnotations का उपयोग करके इस तरह सत्यापन करना भी संभव है?
2 जवाब
जैसा कि r3plica ने कहा था, आप राज्य की जांच के लिए एक कस्टम सत्यापन विधि बना सकते हैं। कस्टम सत्यापन पद्धति में, आप डेटाबेस को क्वेरी कर सकते हैं और जांच सकते हैं कि देश में राज्य है या नहीं और फिर सत्यापन परिणाम लौटाएं। नीचे के रूप में कोड:
public class DeveloperViewModel
{
public int Id { get; set; }
[Required(ErrorMessage = "Name is Required")]
public string Name { get; set; }
[Required(ErrorMessage ="Country is Required")]
public string Country { get; set; }
[RequiredIfHasState("Country", ErrorMessage ="State is Required")]
public string State { get; set; }
}
public class RequiredIfHasStateAttribute : ValidationAttribute
{
private readonly string _comparisonProperty;
public RequiredIfHasStateAttribute(string comparisonProperty)
{
_comparisonProperty = comparisonProperty;
}
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
ErrorMessage = ErrorMessageString;
//get entered state value
var stateValue = (string)value;
var property = validationContext.ObjectType.GetProperty(_comparisonProperty);
if (property == null)
throw new ArgumentException("Property with this name not found");
//get the country value
var countryValue = (string)property.GetValue(validationContext.ObjectInstance);
//get the current dbcontext
var _context = (MvcMovieContext)validationContext.GetService(typeof(MvcMovieContext));
//query the database and check whether the country has state.
if (_context.Countries.Where(c => c.CountryCode == countryValue).Select(c => c).FirstOrDefault().HasState)
{
if(stateValue == null)
{
//if country has state and the state is null. return error message
return new ValidationResult(ErrorMessage);
}
else
{
//if country has state and the state is not found.
if(!_context.Countries.Where(c => c.CountryCode == countryValue).Any(c => c.States.Any(e => e.StateName == stateValue)))
{
return new ValidationResult("State not found");
}
}
}
return ValidationResult.Success;
}
}
नीचे दिया गया स्क्रीनशॉट (GB
देश में राज्य नहीं है, US
देश में राज्य हैं):
यह प्रश्न इससे बहुत मिलता-जुलता है:
कस्टम सत्यापन गुण: एक ही मॉडल में दो गुणों की तुलना करना
मूल रूप से, आप एक कस्टम डेटा विशेषता बनाते हैं। आपके मामले में (छद्म कोड)
public class MyViewModel
{
[RequiredIfHasState("End", ErrorMessage = "State is required.")]
public string StateId { get; set; }
public bool HasState { get; set; }
}
public class RequiredIfHasStateAttribute : ValidationAttribute
{
private readonly string _comparisonProperty;
public RequiredIfHasStateAttribute(string comparisonProperty)
{
_comparisonProperty = comparisonProperty;
}
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
ErrorMessage = ErrorMessageString;
var currentValue = (bool)value;
var property = validationContext.ObjectType.GetProperty(_comparisonProperty);
if (property == null)
throw new ArgumentException("Property with this name not found");
var comparisonValue = (string)property.GetValue(validationContext.ObjectInstance);
if (currentValue && string.IsNullOrEmpty(comparisonValue))
return new ValidationResult(ErrorMessage);
return ValidationResult.Success;
}
}
संबंधित सवाल
जुड़े हुए प्रश्न
नए सवाल
c#
C # (उच्चारण "तेज देखें") Microsoft द्वारा विकसित एक उच्च स्तरीय, सांख्यिकीय रूप से टाइप किया हुआ, बहु-प्रतिमान प्रोग्रामिंग भाषा है। C # कोड आमतौर पर Microsoft के .NET परिवार के टूल और रन-टाइम को लक्षित करता है, जिसमें .NET फ्रेमवर्क, .NET कोर और Xamarin अन्य शामिल हैं। C # या C # के औपचारिक विनिर्देश में लिखे गए कोड के बारे में प्रश्नों के लिए इस टैग का उपयोग करें।