1

I've read David Hayden's great post on MVC 3 Remote validation.

However there is presented what you should do to enable remote (javascript) validation. If the user has javascript disabled the post would still be made even if data is not valid. Therefore a server-side validation should occur.

How could we make this check as DRY (Don't Repeat Yourself) as possible? Of course, including the same check code in the post action as in the remote validation action (or just the same call) can work but I am wondering if a one-liner or something more elegant is available.

Perfectly acceptable answers include "no, it can't be done". :)

4

3 に答える 3

4

私のMSDNの記事を参照してください。方法:ASP.NET MVCでリモート検証を実装 するJavaScriptが無効になっている場合、サーバー側をテストするためにHttpPostCreateメソッドでリモートクライアント検証コードを使用します。

[HttpPost]
    public ActionResult Create(CreateUserModel model) {

        // Verify user name for clients who have JavaScript disabled
        if (_repository.UserExists(model.UserName)) {
            ModelState.AddModelError("UserName", ValidationController.GetAltName(model.UserName, _repository));
            return View("Create", model);
        }
于 2011-05-06T23:05:57.323 に答える
2

'実行可能'ですが、基本的にクライアント側で発行され、サーバー側で検証される独自のカスタム属性を作成する必要があります。私の場合、検証コードをメソッドに抽出してサーバーをチェックするだけです。最近、似たようなものが出てきました。

ASP.NET MVC 3で目立たない検証を使用するときに、フォームが送信されないようにする

リモート属性から継承して、独自のサーバー側コードを追加することはできなかったのでしょうか。うーん..多分私はこれを試してみる必要があります。

ここの誰かがすでにこれをしたと言ったら私は幸せです:)

于 2011-04-23T23:57:51.377 に答える
1

私はこれを行いました、それは少し長い解決策なので、それはすべて私のブログで利用可能です:

http://www.metaltheater.com/tech/technical/fixing-the-remote-validation-attribute/

クラスの新しいサブクラスを作成し、RemoteAttributeから継承して独自のカスタムモデルバインダーを作成してからDefaultModelBinder、リフレクションを使用してコントローラーのバリデーターを呼び出す必要がありました。

于 2013-03-18T06:02:34.523 に答える