我尝试验证IP地址的文本字段。但逻辑不在控制器中。但只是在“正常”课程中:String
。它使用序列化和反序列化来保存数据。但验证是关于放置字段中的IpConditionSettings
,如下所示:
IpAddress
这是<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.EditorFor(model => model.IpCondition.IpAddress, new { htmlAttributes = new { @class = "form-control col-md-3" } })
@*@Html.ValidationMessageFor(model => model.IpCondition.IpAddress, "", new { @class = "text-warning" })*@
</div>
</div>
:
IpAddress
这是javascript:
[SfsHelpers.IpConditionRedirectValidation(ErrorMessageResourceType = typeof(Resources.Entity.Product), ErrorMessageResourceName = "IpAddressWarning")]
[Display(Name = "PreConditionIpAddress", ResourceType = typeof(Resources.Entity.Product))]
public string IpAddress { get; set; }
这是验证类:
<script>
$(function () {
jQuery.validator.addMethod('mustflingsomething',
function (value, element, params) {
return false;
}, '');
jQuery.validator.unobtrusive.adapters.add('mustflingsomething',
function (options) {
options.rules['mustflingsomething'] = {
};
options.messages['mustflingsomething'] = options.message;
});
}(jQuery));
</script>
serialize方法如下所示:
public class IpConditionRedirectValidation : ValidationAttribute, IClientValidatable
{
public IpConditionRedirectValidation() {}
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
IpConditionSettings IpCondition = validationContext.ObjectInstance as IpConditionSettings ;
if ((IpCondition.IpAddress.StartsWith("192")
|| IpCondition.IpAddress.StartsWith("127")))
return new ValidationResult("A monkey must fling something");
if (String.IsNullOrWhiteSpace(IpCondition.IpAddress))
return new ValidationResult(this.FormatErrorMessage(validationContext.DisplayName));
//return new ValidationResult("A monkey must fling something");
return null;
}
#region IClientValidatable Members
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
ModelClientValidationRule mvr = new ModelClientValidationRule();
mvr.ErrorMessage = "Hey, you must fling something - you are a monkey after all";
mvr.ValidationType = "mustflingsomething";
return new[] { mvr };
}
#endregion
}
首先,我想检查internal string Serialize(EditProductModel model)
{
XElement settings = XElement.Parse(model.Product.AuthenticationSettings ?? "<settings/>");
XElement ipaddresses = new XElement("ipaddresses");
if (IpAddress == null)
return string.Empty;
string[] lines = IpAddress.Split(
new string[] { Environment.NewLine },
StringSplitOptions.RemoveEmptyEntries);
lines = IpAddress.Split(new char[0]);
foreach (var item in lines)
{
if (item.StartsWith("127") || item.StartsWith("192"))
//return ("LogOnDetails", model);
return model.Product.AuthenticationSettings;
IPAddress address;// = null;
if (IPAddress.TryParse(item, out address))
{
switch (address.AddressFamily)
{
case System.Net.Sockets.AddressFamily.InterNetwork:
ipaddresses.Add(new XElement("ipaddress", item));
// we have IPv4
break;
case System.Net.Sockets.AddressFamily.InterNetworkV6:
// we have IPv6
ipaddresses.Add(new XElement("ipaddress", item));
break;
default:
break;
}
}
}
settings.Add(ipaddresses);
return settings.ToString();
}
是否以:127或192开头。如果以这些数字开头,则无法保存数据。否则,您可以保存数据。
但问题是现在。您在Ipaddress
字段中输入的每个数字都是错误的。所以你不能保存任何数字
谢谢