是否有任何“内置”方式在MVC5中进行特定于字段的服务器端AJAX验证?如果没有,是否有任何助手可以帮助解决这个问题?

时间:2021-10-26 15:53:26

I often find myself wanting to validate individual form fields on the fly in MVC5 and while I like the DataAnnotations I do not think they offer a way to perform server-side validation out of the box.

我经常发现自己想要在MVC5中动态验证单个表单字段,而我喜欢DataAnnotations,我认为它们不提供开箱即用的服务器端验证方法。

I realize that the validation will be performed on submission anyway but there are certain fields, like username, which the user should be able to see on-the-fly whether they are valid or not. So I keep on writing jQuery/Javascript code to check these fields with $.ajax or similar, and it's a lot of boilerplate code writing. IE, after the appropriate event (input changed) is fired, the client makes a call to the server, then updates a validation placeholder to the right of the field with a checkmark if valid and a bad image if invalid.

我意识到验证将在提交时进行,但有一些字段,如用户名,用户应该能够即时查看它们是否有效。所以我继续编写jQuery / Javascript代码来检查$ .ajax或类似的字段,这是很多样板代码编写。 IE,在触发相应事件(输入已更改)后,客户端调用服务器,然后使用复选标记(如果有效)更新字段右侧的验证占位符,如果无效则更新错误图像。

What bothers me even more is that there is no static type-checking, so I won't know about any egregious syntax errors (ie mis-spelled div id's) until runtime.

令我烦恼的是,没有静态类型检查,所以我不会知道任何令人震惊的语法错误(即错误拼写的div id),直到运行时。

I would like to know:

我想知道:

  1. Is it possible to write a custom attribute that would call the server on submit (just like it would for, say, a [Required] field, except it needs to get a response from the server to proceed)? This would be the best option.
  2. 是否可以编写一个自定义属性来调用提交服务器(就像它需要的那样[必填]字段,除了需要从服务器获取响应才能继续)?这将是最好的选择。

  3. Are there any built-in helpers that can make an Ajax call on some appropriate even (say, onChange) and then update a div after grabbing the result?
  4. 是否有任何内置助手可以在某些适当的偶数(例如onChange)上进行Ajax调用,然后在获取结果后更新div?

  5. If you are an ASP.NET MVC developer, how have you tackled this problem in the past? I don't want to keep on writing boilerplate javascript. I know javascript is unavoidable, but this seems like a generic enough problem that it would be possible to write such a helper.
  6. 如果您是ASP.NET MVC开发人员,那么您过去是如何解决这个问题的呢?我不想继续编写样板javascript。我知道javascript是不可避免的,但这似乎是一个通用的问题,可以编写这样的帮助器。

Thanks in advance for any and all advice.

提前感谢任何和所有建议。

1 个解决方案

#1


0  

You can use the RemoteAttribute.

您可以使用RemoteAttribute。

You specify a method on the controller to be called every time the value changes,and return true if its valid,or error messagee if its not.

您可以在每次值更改时在控制器上指定一个方法,如果有效,则返回true;如果不是,则返回错误消息。

In your model:

在你的模型中:

[Remote("IsUserNameUnique", "User")]
public string UserName { get; set; }

Controller:

public class UserController : Controller
{
    public JsonResult IsUserNameUnique(string UserName)
    {
       if(isUnique(UserName))
          return Json(true,JsonRequestBehavior.AllowGet);
       else 
          retuen Json("Username is taken.",JsonRequestBehavior.AllowGet)
    }
}

And if your view you just put

如果你认为你的观点

@Html.ValidationMessageFor(model=>model.UserName)

#1


0  

You can use the RemoteAttribute.

您可以使用RemoteAttribute。

You specify a method on the controller to be called every time the value changes,and return true if its valid,or error messagee if its not.

您可以在每次值更改时在控制器上指定一个方法,如果有效,则返回true;如果不是,则返回错误消息。

In your model:

在你的模型中:

[Remote("IsUserNameUnique", "User")]
public string UserName { get; set; }

Controller:

public class UserController : Controller
{
    public JsonResult IsUserNameUnique(string UserName)
    {
       if(isUnique(UserName))
          return Json(true,JsonRequestBehavior.AllowGet);
       else 
          retuen Json("Username is taken.",JsonRequestBehavior.AllowGet)
    }
}

And if your view you just put

如果你认为你的观点

@Html.ValidationMessageFor(model=>model.UserName)