簡體   English   中英

ValidationMessageFor 不顯示錯誤信息

[英]ValidationMessageFor not showing an error message

我在這里閱讀了一些類似的問題和答案,但是這兩種解決方案都不能解決我的問題。

問題是驗證可以防止將值保存到數據庫,但不會顯示錯誤消息。 我想我遵循了所有步驟來實現這一點,但它沒有幫助:

Model:


    public class DocumentsModel
    {
       [Required(ErrorMessage = "Choose decision")]
       public string Decision { get; set; }
    
       [Required(ErrorMessage = "Choose date")]
       public DateTime? Manual_date { get; set; }      
    }

看法:


    <td>
    @Html.TextBoxFor(modelItem => item.Manual_date, new { @type = "date", @hidden = "hidden", @Name = "Manual_date", @class = "form-control datepicker" })
    
    @Html.ValidationMessageFor(modelItem => item.Manual_date)
    </td>
    
    <td>
    @Html.DropDownListFor(modelItem => item.Decision, new List<SelectListItem>
             {new SelectListItem { Text="None", Value= "None", Selected=true},
              new SelectListItem { Text="xyz", Value="xyz"}
             }, new { Name = "Decision", @hidden = "hidden" })
    
    @Html.ValidationMessageFor(modelItem => item.Decision)
    </td>

Controller:

    [HttpPost]
    public ActionResult Update(DocumentsModel doc)
            {
             RRRdb db = new RRRdb();
    
             if (ModelState.IsValid)
                {
                 //saving...
                }
             return RedirectToAction("XYZ");
             }
            

腳本:

    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/jqueryval")

Web.config:


      <appSettings>
        <add key="webpages:Version" value="3.0.0.0" />
        <add key="webpages:Enabled" value="false" />
        <add key="ClientValidationEnabled" value="true" />
        <add key="UnobtrusiveJavaScriptEnabled" value="true" />
      </appSettings>

當然,我在需要時使用 jQuery 使下拉列表和文本框可見,因此用戶可以使用這些字段。 正如我在開始時所說的那樣 - 驗證本身有效(不保存字段中的值,除非它們被填充)但消息不會顯示在字段旁邊。 我的代碼有什么問題?

將您的元素放在form標簽中以查看

@model Test.Models.DocumentsModel
<form action="update" method="post">

<div>
    @Html.TextBoxFor(modelItem => modelItem.Manual_date, new { @type = "date", @hidden = "hidden", @Name = "Manual_date", @class = "form-control datepicker" })

    @Html.ValidationMessageFor(modelItem => modelItem.Manual_date)
</div>

<div>
    @Html.DropDownListFor(modelItem => modelItem.Decision, new List<SelectListItem>
             {new SelectListItem { Text="None", Value= "None"},
              new SelectListItem { Text="xyz", Value="xyz"}
             }, "select..", new { Name = "Decision" })

    @Html.ValidationMessageFor(modelItem => modelItem.Decision)
</div>
<div>
    <button type="submit">submit</button>
</div>

</form>

並在您的操作中,如果ModelState.IsValid不為真,則顯示驗證消息返回您的 model;

// Get
    public ActionResult Update()
    {
        ViewBag.Title = "edit";

        return View();
    }

    [HttpPost]
    public ActionResult Update(DocumentsModel doc)
    {
        RRRdb db = new RRRdb();

        if (!ModelState.IsValid)
        {
            return View(doc);
        }
        //saving...
        return RedirectToAction("XYZ");
    }

暫無
暫無

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

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