簡體   English   中英

MVC 客戶端驗證摘要在模型無效時不顯示

[英]MVC Client Side Validation Summary not showing when Model is Invalid

我正在使用名為 MVC Foolproof Validation 的 MVC 驗證 nuget 包。

如果另一個模型屬性為空,我將在我的模型上使用它來將 required 設置為 true 。 驗證部分有效,因為當 Id 字段和 Location 字段留空時,ModelState 被正確設置為無效。 檢查 ModelState 數組上的錯誤,我可以看到它的工作原理。

我的問題是客戶端驗證摘要不顯示。 這是我如何設置的。 誰能發現我的問題?

    [DisplayName("Image Id")]
    public string Id{ get; set; }

    [DisplayName("Location Id")]
    [RequiredIfEmpty("Id", ErrorMessage = "You must..etc"]
    public string LocationId{ get; set; }

在我看來,我正在設置驗證摘要和輸入,如下所示

<div class="form-horizontal">
    <hr/>
    @Html.ValidationSummary(true, "", new {@class = "text-danger"})

    <div class="form-group">
        @Html.LabelFor(model => model.SearchCriteria.Id, htmlAttributes: new {@class = "control-label col-md-2"})
        <div class="col-md-10">
            @Html.EditorFor(model => model.SearchCriteria.Id, new {htmlAttributes = new {@class = "form-control"}})
            @Html.ValidationMessageFor(model => model.SearchCriteria.Id, "", new {@class = "text-danger"})
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.SearchCriteria.LocationId, htmlAttributes: new {@class = "control-label col-md-2"})
        <div class="col-md-10">
            @Html.EditorFor(model => model.SearchCriteria.LocationId, new {htmlAttributes = new {@class = "form-control"}})
            @Html.ValidationMessageFor(model => model.SearchCriteria.LocationId,"", new {@class = "text-danger"})
        </div>
    </div>

在我的控制器操作中,我正在檢查模型狀態。 我需要調用 ModelState.AddModelError(..) 嗎? 我已經試過了,但也許我需要用一種方法來稱呼它。

    [HttpPost]
    public ActionResult Search(SearchCriteria searchCriteria)
    {
        var searchViewModel = new SearchViewModel
        {
            SearchCriteria = searchCriteria
        };

        if (ModelState.IsValid)
        {
            ...
        }
        //ModelState.AddModelError("LocationId", "test");
        return View(searchViewModel);
    }

將 ValidationSummary 輔助行中的布爾參數 (excludePropertyErrors) 更改為false

@Html.ValidationSummary(false, "", new {@class = "text-danger"})

請參閱https://msdn.microsoft.com/de-de/library/ee839464(v=vs.118).aspx

就我而言,我解決了從“ModelOnly”更改為“All”的問題:

來自:

<div asp-validation-summary="ModelOnly" class="text-danger"></div>

至:

<div asp-validation-summary="All" class="text-danger"></div>

我的問題是我對我的行動的呼吁是通過 ajax 完成的。 我真的應該指出,在這里提供幫助的人會立即診斷出問題。 當模型狀態無效時,我正在返回一個新的視圖(searchViewModel),但我必須更新它以返回 json 中的錯誤。

    [HttpPost]
    public ActionResult Search(SearchCriteria searchCriteria)
    {
        if (ModelState.IsValid)
        {
            var searchResults = _searchProvider.GetData(searchCriteria);

            return Json(new
            {
                searchResults
            }, JsonRequestBehavior.AllowGet);
        }

        string errorMessages = string.Join(" <br /> ", this.ModelState.Values
            .SelectMany(v => v.Errors)
            .Select(e => e.ErrorMessage));

        return this.Json(new { HasError = true, ErrorMessages = errorMessages });
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM