1

フォームから FluentValidation を使用していますが、読み込み時に Validation の概要が表示されるようです。フォームを自動的に送信するように見えるものは何もありません。コントローラーでは、フォームが投稿されるまで検証チェックは行われません。奇妙なことに、送信時に検証が完全に機能しているようです。

ビューモデル:

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

2 に答える 2

2

最初に詳細ビューを表示するときは、 を新規作成していると思いますがSendMessageInput、これは新しいため、デフォルトで空Titleになります。

その後、アクションを呼び出しているときは、空のタイトルSendMessageFormでこの新しいものを渡しています。SendMessageInputしたがって、モデル バインド中にモデル エラーが発生するため、部分ビューがレンダリングされると ValidationSummary が表示されます。

の代わりにHtml.Partial(または)を使用してみましたか? これにより、モデル バインディングが行われずにフォームがレンダリングされます。Html.RenderPartialHtml.Action

于 2012-04-13T22:55:23.580 に答える
0

ngmの答えが答えの一部であっても、私はそれを機能させることができませんでした.

したがって、将来これを読んでいる人のために、私がそれを機能させる方法を次に示します。

  • @Html.ValidationSummary()ステートメントに含まれている必要があります@using。そうしないと、検証の概要が表示されません。
  • @Html.Partialorの使用は適切な方法@Html.RenderPartialと考えられていますが、概要の表示を妨げる​​ことはありません。
  • CSSを使用して問題を解決しました。検証の概要は最初にクラスでレンダリングされ、validation-summary-validそれに を適用しdisplay:noneました。エラーが発生すると、クラスは に変更されvalidation-summary-errorsます。

これは、FluentValidation に関連するものではなく、Html 拡張機能が機能する方法です。

于 2012-04-23T13:41:32.070 に答える