使用Angular VS Razor进行ASP.Net MVC验证

时间:2021-05-15 03:21:46

ASP.Net MVC together with Custom Attributes in your model and with Razor, lets you just write an HTML Helper like @Html.ValidationMessageFor(model => model.MyAttribute) to validate each field.

ASP.Net MVC与模型中的自定义属性和Razor一起使用,您可以编写一个HTML Helper,如@ Html.ValidationMessageFor(model => model.MyAttribute)来验证每个字段。

AngularJS intends to render everything on the client side, so we don't have access to Razor and all the javascript validators generated for free. We have to code everything again by hand, and we don't have the guarantee that this validation is in sync with the server side validation, which we had with Razor.

AngularJS打算在客户端呈现所有内容,因此我们无法访问Razor以及免费生成的所有javascript验证器。我们必须手动对所有内容进行编码,并且我们无法保证此验证与我们使用Razor的服务器端验证同步。

How will we overcome this GAP?

我们如何克服这个GAP?

3 个解决方案

#1


4  

This solves my problem:

这解决了我的问题:

https://github.com/alisabzevari/ngval

https://github.com/alisabzevari/ngval

If we want to go deeper (which I don't need now), we can have this:

如果我们想要更深入(我现在不需要),我们可以拥有:

https://bitbucket.org/asiemer/angularjs.mvc/wiki/Home

https://bitbucket.org/asiemer/angularjs.mvc/wiki/Home

#2


0  

I think this is a question Angular beginners will put (this is how I found it :)), and this ishow I think in the end:

我认为这是一个Angular初学者会提出的问题(这就是我发现它:)),这就是我想到底:

The model field to validate gets from the angular view -> to an angular controller -> to an angular service -> to asp.webapi method or asp.mvc controller action, which in the end maps -> to an mvc model

要验证的模型字段从角度视图 - >到角度控制器 - >到角度服务 - >到asp.webapi方法或asp.mvc控制器操作,在最终映射 - >到mvc模型

This means that on all those (at least 4 "projectors") you must be sure to transfer the exact model and field that you will refer with razor.

这意味着在所有这些(至少4个“投影仪”)上,您必须确保传输您将用剃刀引用的确切模型和字段。

So what I mean, is that there are a lot of things that can blow your consistency on the way.

所以我的意思是,有很多东西可以在途中打破你的一致性。

Give up on razor for validation.

放弃剃须刀进行验证。

It's more maintainable (readable) to rewrite them manually for the client-side, and use automated tests to ensure consistency.

为客户端手动重写它们更易于维护(可读),并使用自动化测试来确保一致性。

#3


0  

I will use the server side model for doing server validation only and do all client validation with Angular.

我将使用服务器端模型进行服务器验证,并使用Angular进行所有客户端验证。

The issue is old and does exist before Angular gain popularity. Imagine that you're building your domain model, and you create simple view model for the MVC app. In this case, you have the following alternatives:

问题是陈旧的,并且在Angular获得普及之前确实存在。想象一下,您正在构建域模型,并为MVC应用程序创建简单的视图模型。在这种情况下,您有以下替代方案:

  1. Build your view model on top of the domain model which is not a good solution sometimes. In this case, you can share validation from domain model with view model.
  2. 在域模型之上构建视图模型,这有时不是一个好的解决方案。在这种情况下,您可以使用视图模型从域模型共享验证。
  3. Build the view model decoupled from your domain model which is ideal actually, however in this case you might end up by duplicating validation. Develop validation for server side in the domain model and validation for the UI in the view model.
  4. 构建与您的域模型分离的视图模型,这实际上是理想的,但在这种情况下,您最终可能会重复验证。在域模型中开发服务器端验证,并在视图模型中验证UI。

By view model, I mean the model in the MVC project that's attached to the view.

通过视图模型,我的意思是MVC项目中附加到视图的模型。

In my opinion, I will always go for option 2 and in your case, I'll create client side validation by Angular and server side validation in the domain model.

在我看来,我将始终选择选项2,在您的情况下,我将通过Angular和域模型中的服务器端验证创建客户端验证。

#1


4  

This solves my problem:

这解决了我的问题:

https://github.com/alisabzevari/ngval

https://github.com/alisabzevari/ngval

If we want to go deeper (which I don't need now), we can have this:

如果我们想要更深入(我现在不需要),我们可以拥有:

https://bitbucket.org/asiemer/angularjs.mvc/wiki/Home

https://bitbucket.org/asiemer/angularjs.mvc/wiki/Home

#2


0  

I think this is a question Angular beginners will put (this is how I found it :)), and this ishow I think in the end:

我认为这是一个Angular初学者会提出的问题(这就是我发现它:)),这就是我想到底:

The model field to validate gets from the angular view -> to an angular controller -> to an angular service -> to asp.webapi method or asp.mvc controller action, which in the end maps -> to an mvc model

要验证的模型字段从角度视图 - >到角度控制器 - >到角度服务 - >到asp.webapi方法或asp.mvc控制器操作,在最终映射 - >到mvc模型

This means that on all those (at least 4 "projectors") you must be sure to transfer the exact model and field that you will refer with razor.

这意味着在所有这些(至少4个“投影仪”)上,您必须确保传输您将用剃刀引用的确切模型和字段。

So what I mean, is that there are a lot of things that can blow your consistency on the way.

所以我的意思是,有很多东西可以在途中打破你的一致性。

Give up on razor for validation.

放弃剃须刀进行验证。

It's more maintainable (readable) to rewrite them manually for the client-side, and use automated tests to ensure consistency.

为客户端手动重写它们更易于维护(可读),并使用自动化测试来确保一致性。

#3


0  

I will use the server side model for doing server validation only and do all client validation with Angular.

我将使用服务器端模型进行服务器验证,并使用Angular进行所有客户端验证。

The issue is old and does exist before Angular gain popularity. Imagine that you're building your domain model, and you create simple view model for the MVC app. In this case, you have the following alternatives:

问题是陈旧的,并且在Angular获得普及之前确实存在。想象一下,您正在构建域模型,并为MVC应用程序创建简单的视图模型。在这种情况下,您有以下替代方案:

  1. Build your view model on top of the domain model which is not a good solution sometimes. In this case, you can share validation from domain model with view model.
  2. 在域模型之上构建视图模型,这有时不是一个好的解决方案。在这种情况下,您可以使用视图模型从域模型共享验证。
  3. Build the view model decoupled from your domain model which is ideal actually, however in this case you might end up by duplicating validation. Develop validation for server side in the domain model and validation for the UI in the view model.
  4. 构建与您的域模型分离的视图模型,这实际上是理想的,但在这种情况下,您最终可能会重复验证。在域模型中开发服务器端验证,并在视图模型中验证UI。

By view model, I mean the model in the MVC project that's attached to the view.

通过视图模型,我的意思是MVC项目中附加到视图的模型。

In my opinion, I will always go for option 2 and in your case, I'll create client side validation by Angular and server side validation in the domain model.

在我看来,我将始终选择选项2,在您的情况下,我将通过Angular和域模型中的服务器端验证创建客户端验证。