2012-04-13 3 views
1

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> 
} 

답변

2

처음에는 세부보기를 표시 할 때 SendMessageInput을 새로 작성하는 것이므로 새로 작성하면 기본값으로 Title이 비어 있습니다.

SendMessageForm 동작으로 전화를 걸면이 새로운 SendMessageInput에 빈 제목이 전달됩니다. 따라서 모델 바인딩 중에 모델 오류가 발생하므로 부분 뷰가 렌더링되면 ValidationSummary가 표시됩니다.

Html.Action 대신 Html.Partial (또는 Html.RenderPartial)을 사용해 보셨습니까? 이렇게하면 모델 바인딩이 수행되지 않고 양식이 렌더링됩니다.

+0

좋아요, 시도해보십시오! 감사! – Allov

+0

음, Partial AND RenderPartial은 여전히 ​​동일한 결과를 보여줍니다. 이상하게 보입니다 = ( – Allov

+0

내 프로젝트에 두 가지 형식이 있었는데 Html.ValidationSummer()를 사용하는 것 외에도 Html.Action ...을 사용하는 것보다 (이 중 하나는 현재보고있는 것입니다.) 그리고 다른 하나는 Html.ValidationSummery() 사용하여 내부,하지만 Html.Partial 사용하고 ... 그래서 ... 결론, 당신은 Html.ValidationSummary() 사용 및 외부 Html.Partial 사용해야합니다. 감사합니다 대답! – Allov

0

ngm의 답변이 답변의 일부인 경우에도 제대로 작동하지 못했습니다.

그래서, 앞으로이 글을 읽고 있어요 누군가를 위해, 여기에 내가 그것이 작동되도록 않았다 방법은 다음과 같습니다
  • @Html.ValidationSummary()

    @using 문에 있어야합니다 아니면 그냥 검증 요약을 표시하지 않습니다.
  • @Html.Partial 또는 @Html.RenderPartial을 사용하면 좋은 요약 정보로 간주되지만 요약 정보는 표시되지 않습니다.
  • CSS를 사용하여 문제를 해결했습니다. 유효성 검사 요약은 먼저 validation-summary-valid 클래스로 렌더링되고 display:none을 적용했습니다. 오류가 발생하면 클래스는 validation-summary-errors으로 변경됩니다.

FluentValidation과 관련이 없으며, HTML 확장이 작동하는 방식 일뿐입니다.