I’m using FluentValidation form a form, and the Validation summary seems to be displayed at load time. I don’t have anything that seems to submit the form automatically and in my controller, no validation check is made until the form is posted. Weird enough, validation seems to work perfectly when submiting.
ViewModel:
[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"));
}
}
Controller:
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
});
}
View (Main):
@Html.Action("SendMessageForm", "SendMessage", Model.SendMessageInput)
View (Partial):
@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>
}
I think when you’re first going to display the Detail view, you are newing up a
SendMessageInput, which as it is new will have an emptyTitleby default.When you are then calling through to the
SendMessageFormaction, you are passing it this newSendMessageInputwith the empty title. Thus during model binding it will be getting a model error, and hence when your partial view is rendered the ValidationSummary is displayed.Have you tried using
Html.Partial(orHtml.RenderPartial) instead ofHtml.Action? This would render the form without any model binding taking place.