-1
我为我的MVC 5应用程序中的一个特定字段构建了自定义验证规则。它在编辑表单上效果很好,但在“创建”表单上验证相同字段时,客户端验证不会触发 - 客户端验证被触发,但在创建窗体上显示为有效,即使我可以看到它是不是,所以不会显示任何消息。MVC不显眼的自定义规则只适用于客户端的一种形式
这两种形式都使用相同的模型。 的脚本在_layout页面添加使这两种观点都有的所有脚本。 这两种观点都具有完全相同的剃刀代码包括ValidationMessageFor()
当表单到达控制器时,由于自定义错误,模型无效,所以验证在服务器端工作,但不在客户端,我无法找到anythi这将使它以一种形式工作,但不是另一种形式。
这里是我的代码:
自定义属性:
public class AtLeastOneRequiredAttribute : ValidationAttribute, IClientValidatable
{
public string OtherPropertyNames;
public AtLeastOneRequiredAttribute(string otherPropertyNames)
{
OtherPropertyNames = otherPropertyNames;
}
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
string[] propertyNames = OtherPropertyNames.Split(',');
bool IsAllNull = true;
foreach(var i in propertyNames)
{
var p = validationContext.ObjectType.GetProperty(i);
var val = p.GetValue(validationContext.ObjectInstance, null);
if(val != null && val.ToString().Trim() != "")
{
IsAllNull = false;
break;
}
}
if(IsAllNull)
{
return new ValidationResult(FormatErrorMessage(validationContext.DisplayName));
}
else
{
return null;
}
}
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
var rules = new ModelClientValidationRule()
{
ErrorMessage = FormatErrorMessage(metadata.DisplayName),
ValidationType = "atleastonerequired"
};
rules.ValidationParameters["otherpropertynames"] = OtherPropertyNames;
yield return rules;
}
}
客户端代码:
$(function() {
$.validator.unobtrusive.adapters.addSingleVal("atleastonerequired", "otherpropertynames");
$.validator.addMethod("atleastonerequired", function (value, element, params) {
var param = params.toString().split(',');
var IsAllNull = true;
$.each(param, function (i, val) {
var valueOfItem = $('#Activity_' + val).val().trim();
if (valueOfItem != '') {
IsAllNull = false;
return false;
}
});
if (IsAllNull) {
return false;
}
else {
return true;
}
})
})
查看 - 编辑&创建形式是相同的:
@using (Html.BeginForm("Edit", "Activity", FormMethod.Post, new { @id = "editActivityForm" }))
{
@Html.AntiForgeryToken()
<div class="form activity-form">
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.Activity.RecordId)
<div class="form-group">
@Html.LabelFor(model => model.Activity.Acres, htmlAttributes: new { @class = "control-label" })
@Html.EditorFor(model => model.Activity.Acres, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Activity.Acres, "", new { @class = "text-danger" })
</div>
<div class="form-group">
@Html.LabelFor(model => model.Activity.Volume, htmlAttributes: new { @class = "control-label" })
@Html.EditorFor(model => model.Activity.Volume, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Activity.Volume, "", new { @class = "text-danger" })
</div>
<div class="form-group">
@Html.LabelFor(model => model.Activity.Feet, htmlAttributes: new { @class = "control-label" })
@Html.EditorFor(model => model.Activity.Feet, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Activity.Feet, "", new { @class = "text-danger" })
</div>
<div class="form-group">
@Html.LabelFor(model => model.Activity.Hours, htmlAttributes: new { @class = "control-label" })
@Html.EditorFor(model => model.Activity.Hours, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Activity.Hours, "", new { @class = "text-danger" })
</div>
<div class="form-group">
@Html.LabelFor(model => model.Activity.Comment, htmlAttributes: new { @class = "control-label" })
@Html.EditorFor(model => model.Activity.Comment, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Activity.Comment, "", new { @class = "text-danger" })
</div>
<div class="form-group">
<input type="submit" value="Save" class="btn btn-primary" onclick="$.validator.unobtrusive.parse($('#editActivityForm'));" />
</div>
</div>
}
加入attirbute的型号:
[AtLeastOneRequired("Acres,Volume,Feet,Hours", ErrorMessage = "Activity requires at least one measure - Acres, Volume, Feet or Hours.")]
public Nullable<int> Acres { get; set; }
public Nullable<int> Volume { get; set; }
public Nullable<int> Feet { get; set; }
public Nullable<int> Hours { get; set; }
我很高兴补充说明或更多信息。我不知道还有什么要补充的。我可以粘贴所有的代码,但代码对于两种表单都是一样的,所以不知道如何帮助。你需要什么来帮助我? – BattlFrog