-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