ValidationMessageFor不显示错误消息 [英] ValidationMessageFor not showing an error message

查看:89
本文介绍了ValidationMessageFor不显示错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在此处已经阅读了一些类似的问题和答案,但是没有一种解决方案可以解决我的问题.

I've read a few similar questions and answers here on SO but neither solution solves my problem.

问题在于,验证的工作是防止将值保存到db,但不会显示错误消息.我想我已按照所有步骤实现了该目标,但这无济于事:

The issue is that validation works in terms of preventing from saving values to db but error messages don't show up. I think I followed all steps how to achieve that but it doesn't help:

型号:


    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>

控制器:

    [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时,我会让dropdownlist和textbox可见,以便用户可以使用这些字段.正如我在开始时所说的那样,验证本身可以工作(除非填充字段,否则不保存字段中的值),但是消息不会显示在字段旁边.我的代码有什么问题?

Of course I make dropdownlist and textbox visible when needed using jQuery so user can use those fields. As I said in the beginning - validation itself works (not save values from fields unless they are filled) but messages won't show up next to fields. What's wrong with my code?

推荐答案

将元素放入 view

@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 不是true,则显示验证消息会返回您的模型;

and in your action, to display validation messages return your model if ModelState.IsValid is not true;

// 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");
    }

这篇关于ValidationMessageFor不显示错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆