我在ASP.NET MVC应用程序中使用Fluent验证。当出现错误时,我需要更改控件的CSS类。请参阅下面的代码。
using FluentValidation;
using Hrms.Framework.Common;
using Hrms.Web.Models;
namespace Hrms.Web.InputValidators
{
public class AnnouncementValidator : AbstractValidator<AnnouncementModel>
{
public AnnouncementValidator()
{
RuleFor(a => a.Title).NotEmpty().WithMessage("Announcement Title is required");
RuleFor(a => a.Title).MaximumLength(50).WithMessage(string.Format("Announcement Title should be 50 characters or less", 50));
}
}
}
以下是我在视图中使用的代码(HTML)
@model Hrms.Web.Models.AnnouncementModel
@{
Layout = "~/Views/Shared/_Application.cshtml";
}
@using (Ajax.BeginForm("Save", "Announcement", new AjaxOptions
{
HttpMethod = "POST",
OnFailure = "saveFailed",
OnSuccess = "saveSucc",
InsertionMode = InsertionMode.Replace,
UpdateTargetId = "result"
}))
{
@Html.AntiForgeryToken()
<div id="result"></div>
<label>@Resources.Organization.Announcement.Title</label>
@Html.TextBoxFor(m => m.Title, new { maxlength = @GlobalConstants.AnnouncementTitleMaxLength })
@Html.ValidationMessageFor(model => model.Title, null, new { @class = "errVal" })
<input type="submit" value="Save">
}
@section scripts {
<script type="text/javascript">
$(document).ready(function () {
$("#SendingDate").datepicker();
});
function saveSucc(m) {
$("#Title").val("");
$("#Body").val("");
$("#SendingDate").val("");
}
function saveFailed() {
alert("saveFailed");
}
</script>
}
非常感谢任何帮助
当我尝试编辑这篇文章时,它显示错误,说我无法编辑此帖子,因为它主要是代码,它要求我添加更多详细信息。而且由于我不想添加更多细节,我决定添加此文本。对不起......