我正在使用表单中的FluentValidation,并且验证摘要似乎在加载时显示。我没有任何似乎自动提交表单的内容,在我的控制器中,在发布表单之前不会进行任何验证检查。很奇怪,验证似乎在提交时效果很好。
视图模型:
[Validator(typeof(SendMessageInputValidator))]
public class SendMessageInput
{
public string Title { get; set; }
public string Content { get; set; }
public string VideoUrl { get; set; }
public string CultureName { get; set; }
public bool VideoMode { get; set; }
}
public class SendMessageInputValidator : AbstractValidator<SendMessageInput>
{
public SendMessageInputValidator()
{
RuleFor(s => s.Title)
.NotEmpty().WithMessage("TitleRequired".Translate("MCN"));
}
}
控制器:
public ActionResult Detail(Guid entityId, string cultureName)
{
var entity = _sendMessageRepository.Get(entityId);
if (entity == null)
throw new HttpException(404, "Not found.");
return View(new SendMessagePageViewModel
{
NodeId = entity.NodeId,
Name = entity.Name,
Title = entity.Title,
Content = entity.Content,
BrowserTitle = entity.BrowserTitle,
MetaDescription = entity.MetaDescription,
MetaKeywords = entity.MetaKeywords,
SendMessageInput = new SendMessageInput { VideoMode = true }
});
}
public ActionResult SendMessageForm(SendMessageInput input)
{
input.CultureName = Thread.CurrentThread.CurrentUICulture.Name;
return PartialView(/*input*/ new SendMessageInput());
}
[HttpPost]
public ActionResult SendMessage(SendMessageInput input)
{
Thread.CurrentThread.CurrentUICulture = new CultureInfo(input.CultureName);
if (ModelState.IsValid)
{
return Redirect(Utilities.GetUrl(Constants.NodeIds.MyProfile));
}
var entity = _sendMessageRepository.Get(Constants.NodeIds.MentorQuestionForm);
if (entity == null)
throw new HttpException(404, "Not found.");
return PartialView("Detail", new SendMessagePageViewModel
{
NodeId = entity.NodeId,
Name = entity.Name,
Title = entity.Title,
Content = entity.Content,
BrowserTitle = entity.BrowserTitle,
MetaDescription = entity.MetaDescription,
MetaKeywords = entity.MetaKeywords,
SendMessageInput = input
});
}
查看(主要):
@Html.Action("SendMessageForm", "SendMessage", Model.SendMessageInput)
查看(部分):
@Html.ValidationSummary(false, "ValidationSummaryHeader".Translate("MCN"))
@using (Html.BeginForm("SendMessage", "SendMessage", FormMethod.Post))
{
<div class="Formulaire">
<p>
@Html.LabelFor(m => m.Title, "Title".Translate("MCN"), true)
@Html.TextBoxFor(m => m.Title, new { maxlength = 200, @class = "TxtBox" })
</p>
@if (Model.VideoMode)
{
<p>
@Html.LabelFor(m => m.VideoUrl, "VideoUrl".Translate("MCN"))
@Html.TextBoxFor(m => m.VideoUrl)
</p>
}
else
{
<p>
@Html.LabelFor(m => m.Content, "Message".Translate("MCN"))
@Html.TextAreaFor(m => m.Content, new { @class = "TxtArea" })
</p>
}
@Html.HiddenFor(m => m.CultureName)
<input type="submit" value="@("Submit".Translate("MCN"))"/>
</div>
}
答案 0 :(得分:2)
我认为当您第一次显示详细信息视图时,您正在新建SendMessageInput
,因为它是新的,默认情况下会为空Title
。
当您调用SendMessageForm
操作时,您将使用空标题向新SendMessageInput
传递。因此,在模型绑定期间,它将获得模型错误,因此当呈现部分视图时,将显示ValidationSummary。
您是否尝试过使用Html.Partial
(或Html.RenderPartial
)代替Html.Action
?这将使表单不会发生任何模型绑定。
答案 1 :(得分:0)
即使ngm的答案是答案的一部分,我也无法使其发挥作用。
所以,对于那些将来读这篇文章的人来说,这就是我的工作方式:
@Html.ValidationSummary()
必须位于@using
语句中,否则它不会显示验证摘要。@Html.Partial
或@Html.RenderPartial
被认为是一个很好的实践,但不会阻止显示摘要。validation-summary-valid
呈现,然后我将display:none
应用于它。一旦出现错误,该课程将更改为validation-summary-errors
。在那里,你拥有它,甚至与FluentValidation无关,它只是Html扩展的工作方式。