MVC AddModelError not showing up

本文关键字:up showing not AddModelError MVC | 更新日期: 2023-09-27 18:19:32

我看了这篇SO文章,按照第一个答案做了什么,我也做了同样的事情,但我仍然看不到出现的错误。

模型的相关部分是:

[Display(Name = "Verify13", ResourceType = typeof(Resources.Resource))]
public bool Is13 { get; set; }

控制器为:

if (registerModel.Is13 == false)
{
    ModelState.AddModelError("REGISTER_0", "You must be 13 of age or older to receive email updates.");
    return View(registerModel);
}

视图的一部分是:

@Html.ValidationSummary(true)
...
@Html.LabelFor(model => model.Is13)
@Html.CheckBoxFor(model => model.Is13)
@Html.ValidationMessage("REGISTER_O")

我做错了什么?模型的其余部分在字段上具有属性。整个模型如下所示。错误消息(字段为必填项)。除了这个错误,所有的错误都会出现。

public class RegisterViewModel
{
    [Display(Name = "FirstName", ResourceType = typeof(Resources.Resource))]
    [Required(ErrorMessageResourceType = typeof(Resources.Resource),
              ErrorMessageResourceName = "FirstNameRequired")]
    [StringLength(50, ErrorMessageResourceType = typeof(Resources.Resource),
                      ErrorMessageResourceName = "FirstNameLong")]
    public string FirstName { get; set; }
    [Display(Name = "LastName", ResourceType = typeof(Resources.Resource))]
    [Required(ErrorMessageResourceType = typeof(Resources.Resource),
              ErrorMessageResourceName = "LastNameRequired")]
    [StringLength(50, ErrorMessageResourceType = typeof(Resources.Resource),
                      ErrorMessageResourceName = "LastNameLong")]
    public string LastName { get; set; }
    [Display(Name = "Email", ResourceType = typeof(Resources.Resource))]
    [Required(ErrorMessageResourceType = typeof(Resources.Resource),
              ErrorMessageResourceName = "EmailRequired")]
    [RegularExpression(".+@.+''..+", ErrorMessageResourceType = typeof(Resources.Resource),
                                     ErrorMessageResourceName = "EmailInvalid")]
    public string Email { get; set; }
    [Display(Name = "ConfirmEmail", ResourceType = typeof(Resources.Resource))]
    [Required(ErrorMessageResourceType = typeof(Resources.Resource),
            ErrorMessageResourceName = "ConfirmEmailRequired")]
    [RegularExpression(".+@.+''..+", ErrorMessageResourceType = typeof(Resources.Resource),
                                     ErrorMessageResourceName = "EmailInvalid")]
    public string ConfirmEmailAddress { get; set; }
    [Display(Name = "Password", ResourceType = typeof(Resources.Resource))]
    [Required(ErrorMessageResourceType = typeof(Resources.Resource),
              ErrorMessageResourceName = "PasswordRequired")]
    public string Password { get; set; }
    [Display(Name = "ConfirmPassword", ResourceType = typeof(Resources.Resource))]
    [Required(ErrorMessageResourceType = typeof(Resources.Resource),
              ErrorMessageResourceName = "ConfirmPasswordRequired")]
    public string ConfirmPassword { get; set; }
    [Display(Name = "ZipCode", ResourceType = typeof(Resources.Resource))]
    [Required(ErrorMessageResourceType = typeof(Resources.Resource),
              ErrorMessageResourceName = "ZipCodeRequired")]
    public string ZipCode { get; set; }
    public string[] VideoProvider { get; set; }
    public IEnumerable<SelectListItem> Items { get; set; }
    [Display(Name = "Verify13", ResourceType = typeof(Resources.Resource))]
    public bool Is13 { get; set; }

MVC AddModelError not showing up

或者您可以装饰您的房产。

private bool isTrue
{ get { return true; } }
[Required]
[Display(Name = "I agree to the terms and conditions")]
[Compare("isTrue", ErrorMessage = "Please agree to Terms and Conditions")]
public bool iAgree { get; set; }

这是@fields.cage在这里回答

这样,你就可以将验证逻辑放在它应该放的位置,并且你可以像其他逻辑一样绑定它

编辑

在给出输出的代码下面,我想您希望看到。为了简单起见,我只实现了名字和13字段。

型号:

public class RegisterViewModel
{
    [Display(Name = "FirstName")]
    [Required]
    public string FirstName { get; set; }
    public bool isTrue
    { get { return true; } }
    [Display(Name = "Verify13")]
    [Required]
    [System.Web.Mvc.CompareAttribute("isTrue", ErrorMessage = "You must be 13 of age or     older to receive email updates.")]
    public bool Is13 { get; set; }
}

视图:

@using (Html.BeginForm("Index", "Home"))
{
    <table>
        <tr>
            <td>@Html.LabelFor(s => s.FirstName)</td>
            <td>@Html.TextBoxFor(s => s.FirstName)</td>
            <td>@Html.ValidationMessageFor(s => s.FirstName)</td>
        </tr>
        <tr>
            <td>@Html.LabelFor(s => s.Is13)</td>
            <td>@Html.CheckBoxFor(s => s.Is13)</td>
            <td>@Html.ValidationMessageFor(s => s.Is13)</td>
        </tr>
    </table>
    <button type="submit">Send</button>
}

控制器:

[HttpPost]
public ActionResult Index(RegisterViewModel model)
{
    if (!ModelState.IsValid)
    {
        return View(model);
    }
    return View(model);
}

您是否将这两个javascript文件添加到视图中(用于客户端验证):

~/Scripts/jquery.validate.min.js
~/Scripts/jquery.validate.unobtrusive.min.js

如果你使用的是Ajax.BeginForm()函数,你还需要这个文件:

~/Scripts/jquery.unobtrusive-ajax.min.js