简体   繁体   English

Html.ValidationSummary中不显示自定义验证错误消息

[英]Custom validation error message doesn't display in Html.ValidationSummary

I've created a custom validation attribute to apply to my view model. 我创建了一个自定义验证属性以应用于我的视图模型。 In my form, I have a @Html.ValidationSummary control. 在我的表单中,我有一个@Html.ValidationSummary控件。 When my custom validation attribute is invalid, the summary control doesn't display the error message for some reason. 当我的自定义验证属性无效时,由于某些原因,摘要控件不会显示错误消息。

Here's my custom validator: 这是我的自定义验证器:

public class UserFolderExistsAttribute : ValidationAttribute
{
    private const string _defaultErrorMessage = 
        "A folder with this name already exists";
    private readonly object _typeId = new object();

    public UserFolderExistsAttribute(string folderName) :
        base(_defaultErrorMessage)
    {
        FolderName = folderName;
    }

    public string FolderName { get; private set; }
    public override object TypeId { get { return _typeId; } }

    public override bool IsValid(object value)
    {
        return false; // return error message to test for now
    }
}

Here's my view model, with my custom validator attribute applied: 这是我的视图模型,应用了我的自定义验证器属性:

[UserFolderExists("Name")]
public class UserFolderViewModel
{
    [Required(ErrorMessage = "Name is required")]
    public string Name { get; set; }
}

Here's my partial view: 这是我的部分观点:

@using (Ajax.BeginForm("Create", "Folders",
    new AjaxOptions { OnSuccess = "OnSuccess" }))
{
    @Html.AntiForgeryToken()

    @Html.TextBoxFor(m => m.Name, new { placeholder = "Name" })

    <p>@Html.ValidationSummary()</p>

    <p><input type="submit" class="create" value="" /></p>
}

Here's the method my form is posting to: 这是我的表单发布到的方法:

[HttpPost]
public JsonResult Create(UserFolderViewModel viewModel)
{
    if (ModelState.IsValid)
    {
        // do something
    }

    return Json("error");  
}

The ModelState.IsValid attribute returns false, so it recognizes my custom validator. ModelState.IsValid属性返回false,因此可以识别我的自定义验证器。 But the summary control doesn't automatically display my message. 但是摘要控件不会自动显示我的消息。 The summary does recognize the Required data annotation validator, and displays the error message. 摘要不会识别“ Required数据注释验证器”,并显示错误消息。

How can I get the Validation Summary to display my custom error message? 如何获得验证摘要以显示我的自定义错误消息?

You are returning a JsonResult object with just a string "error" inside, how is MVC able to know what validation message to show on the client-side? 您将返回一个JsonResult对象,其中仅包含一个字符串"error" ,MVC如何才能知道要在客户端显示的验证消息? If you use normal posting (with ActionResult ) you can just return the same model and the validation messages will appear: 如果您使用普通的发布(与ActionResult ),则可以返回相同的模型,并且将显示验证消息:

return View(viewModel);

You can also validate the object yourself in the controller and return an error message through the JsonResult class by using return Json("error message here"); 您也可以自己在控制器中验证对象,并使用return Json("error message here");通过JsonResultreturn Json("error message here");

Or you could try to get the validation error messages from the ModelState property and return them with Json. 或者,您可以尝试从ModelState属性获取验证错误消息,并使用Json返回它们。 Check out the second answer of this question . 查看此问题的第二个答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM