我有一个文本字段,用户可以在其中填写IP地址。但有时用户填写错误的IP地址。因此,对于填写的每个错误的IP地址,都会显示一条消息,IP地址错误。
我正在使用这种方法:
protected override IEnumerable <ValidationResult> IsValid(object value, ValidationContext validationContext) {
IpConditionSettings IpCondition = validationContext.ObjectInstance as IpConditionSettings ;
string[] lines = IpCondition.IpAddress.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
lines = IpCondition.IpAddress.Split(new char[0], StringSplitOptions.RemoveEmptyEntries);
foreach (var item in lines)
{
if (item.StartsWith("127") || item.StartsWith("192"))
for (int i = 0; i < item.Length; i++)
{
//return model.Product.AuthenticationSettings;
item[i].ToString();
yield return new ValidationResult("IpAddress cant be saved");
}
// return new ValidationResult("IpAddress cant be saved");
if (String.IsNullOrWhiteSpace(IpCondition.IpAddress))
yield return new ValidationResult(this.FormatErrorMessage(validationContext.DisplayName));
IPAddress address;
if (IPAddress.TryParse(item, out address))
{
continue;
}
else
{
yield return new ValidationResult("IpAddress cant be saved");
}
}
yield return null;
}
这是消息:
foreach (var item in lines)
{
if (item.StartsWith("127") || item.StartsWith("192"))
for (int i = 0; i < item.Length; i++)
{
//return model.Product.AuthenticationSettings;
item[i].ToString();
yield return new ValidationResult("IpAddress cant be saved");
}
// return new ValidationResult("IpAddress cant be saved");
if (String.IsNullOrWhiteSpace(IpCondition.IpAddress))
yield return new ValidationResult(this.FormatErrorMessage(validationContext.DisplayName));
IPAddress address;
if (IPAddress.TryParse(item, out address))
{
continue;
}
else
{
yield return new ValidationResult("IpAddress cant be saved");
}
}
但是我收到了这条消息:
'SenecaFormsServer.SfsHelpers.IpConditionRedirectValidation.IsValid(object, System.ComponentModel.DataAnnotations.ValidationContext)':
这是Ipaddress的观点:
<div class="editor-multiline-field">
<div class="form-group" id="hallo">
@Html.LabelFor(model => model.IpCondition.IpAddress, new { @class = "text-bold control-label col-md-2" })
<div class="col-lg-6 col-md-8 col-sm-10" id="hallo">
@Html.TextAreaFor(model => model.IpCondition.IpAddress, new { htmlAttributes = new { @class = "form-control col-md-3" } })
@Html.ValidationMessageFor(model => model.IpCondition.IpAddress, "", new { @class = "text-error" })
</div>
</div>
</div>
我现在在视图中有这个:
<div class="editor-multiline-field">
<div class="form-group" id="hallo">
@Html.LabelFor(model => model.IpCondition.IpAddress, new { @class = "text-bold control-label col-md-2" })
<div class="col-lg-6 col-md-8 col-sm-10" id="hallo">
@Html.TextAreaFor(model => model.IpCondition.IpAddress, new { htmlAttributes = new { @class = "form-control col-md-3" } })
<br />
@for (int i = 0; i < Model.IpCondition.IpAddress.Count() ; i++)
{
<br />
@Html.ValidationMessageFor(model => model.IpCondition.IpAddress, "", new { @class = "text-error" })
}
</div>
</div>
</div>
这是我的方法:
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
IpConditionSettings IpCondition = validationContext.ObjectInstance as IpConditionSettings ;
string[] lines = IpCondition.IpAddress.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
lines = IpCondition.IpAddress.Split(new char[0], StringSplitOptions.RemoveEmptyEntries);
foreach (var item in lines)
{
if (item.StartsWith("127") || item.StartsWith("192"))
foreach (var result in lines)
{
result.ToString();
return new ValidationResult("IpAddress cant be saved");
}
// return new ValidationResult("IpAddress cant be saved");
if (String.IsNullOrWhiteSpace(IpCondition.IpAddress))
return new ValidationResult(this.FormatErrorMessage(validationContext.DisplayName));
IPAddress address;
if (IPAddress.TryParse(item, out address))
{
continue;
}
else
{
return new ValidationResult("IpAddress cant be saved");
}
}
return null;
}
但现在我得到错误消息的六倍。例如,我填写:127和192然后我必须收到两条错误消息。