FluentValidation 양식을 사용하고 있으며 유효성 검사 요약이로드 할 때 표시되는 것 같습니다. 양식을 자동으로 제출하는 것으로 보이는 것이 없으며 양식이 게시 될 때까지 유효성 확인이 이루어지지 않습니다. 이상하게도 유효성 검사는 제출할 때 완벽하게 작동하는 것 같습니다.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>
}
좋아요, 시도해보십시오! 감사! – Allov
음, Partial AND RenderPartial은 여전히 동일한 결과를 보여줍니다. 이상하게 보입니다 = ( – Allov
내 프로젝트에 두 가지 형식이 있었는데 Html.ValidationSummer()를 사용하는 것 외에도 Html.Action ...을 사용하는 것보다 (이 중 하나는 현재보고있는 것입니다.) 그리고 다른 하나는 Html.ValidationSummery() 사용하여 내부,하지만 Html.Partial 사용하고 ... 그래서 ... 결론, 당신은 Html.ValidationSummary() 사용 및 외부 Html.Partial 사용해야합니다. 감사합니다 대답! – Allov