问题标签 [asp.net-mvc-validation]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
1879 浏览

c# - 从服务器端的路由值解析 url

我正在尝试创建一个 ValidationAttribute (用于仅在服务器端有效的远程验证),并且在 IsValid 方法中,我需要从 url 路由值解析 url。这是我的初始设置:

有什么想法吗?

0 投票
1 回答
5688 浏览

asp.net - 自定义验证:从 validationContext 获取属性名称

对于我的 ASP.NET MVC 项目,我创建了一个自定义验证属性。这是我正在努力的代码:

您可以从以下链接查看完整代码:

https://bitbucket.org/tugberk/tugberkug.mvc/src/6cc3d3d64721/TugberkUg.MVC/Validation/ServerSideRemoteAttribute.cs

对于fullUrl变量,我试图将属性名称附加到查询字符串,但是当我使用时validationContext.MemberName,我失败了。我通过将其设为静态作为“术语”解决了临时修复的问题,但它根本不是修复。

那么,从 中检索属性名称的方法是什么validationContext

0 投票
2 回答
1128 浏览

asp.net - DataTypeAttribute 是 DefaultModelBinder 类的验证属性吗

我刚刚注意到DataTypeAttribute该类是从System.ComponentModel.DataAnnotations.ValidationAttribute.

就 ASP.NET MVCDefaultModelBinder类而言,DataTypeAttribute是一个验证属性吗?简而言之,ModelBinder 是否根据DataTypeAttribute

例如,如果我将DataType属性指定为DataType.EmailAddress,它将验证电子邮件地址还是此属性仅提供对象的元数据。

更新

我在 SO 上发现了一个类似的问题:

DataTypeAttribute 验证是否在 MVC2 中工作?

因此,据此它不能用作验证属性。System.ComponentModel.DataAnnotations.ValidationAttribute那么,如果它不用作验证属性,为什么要继承它呢?

0 投票
1 回答
1766 浏览

asp.net-mvc - 没有验证属性的部分视图(ASP.NET MVC 3)

在我的一个 ASP.NET MVC 3 应用程序上不断发生一件奇怪的事情。

我正在通过 jQuery Ajax api 获取插入行,这没有问题。但是当我得到必要的局部视图时,它没有验证属性,我无法重新绑定这些行的验证。

这是我得到的ajax响应:

这是我应该得到的:

GUID 不必相同。我正在做所谓的非顺序绑定。

这是我通过 jquery ajax 调用以获取新插入行的操作:

我几乎想不通为什么会发生这种情况。任何的想法?

0 投票
5 回答
7157 浏览

asp.net-mvc - ASP.net MVC 中的流利验证 - 数据库验证

我在我的 ASP.net MVC 3 项目中使用 Fluent Validation 框架。到目前为止,我的所有验证都非常简单(确保字符串不为空,只有一定长度等),但现在我需要验证数据库中是否存在某些内容。

  1. 在这种情况下应该使用 Fluent Validation 吗?
  2. 如果应该使用 Fluent Validation 完成数据库验证,那么我该如何处理依赖关系?验证器类是自动创建的,我需要以某种方式将它传递给我的存储库实例之一才能查询我的数据库。

我试图验证的一个例子可能是:

我的页面上有一个下拉列表,其中包含所选项目的列表。我想在尝试保存新记录之前验证他们选择的项目是否确实存在于数据库中。

编辑
这是 Fluent Validation 框架中常规验证的代码示例:

控制器:

如您所见,我从未自己创建验证器。这是因为以下行Global.asax

问题是现在我有一个需要使用存储库与我的数据库交互的验证器,但是由于我没有创建验证器,所以除了硬编码具体类型之外,我不知道如何传入该依赖项。

0 投票
3 回答
2781 浏览

jquery - How to use asp.net mvc 3 unobtrusive javascript validation with a wizard?

I am using asp.net mvc 3 and want to make use of unobtrusive javascript . I am desiging a form that is quite long and probably would be very overwhelming if not broken up.

I decided to use jquery form wizard to break it into a wizard that should not look so overwhelming. I am not wondering how do I use the data annotations and unobtrusive java-script with wizard.

Right now everything is still in one big form but just looks broken up through the wizard. There is only one submit button at the end of the form that when clicked will serialize the form data and do an ajax post to the server with that data.

From what I seen usually for the first time when using the mvc validation the user needs to click the submit button before any of the client side validation gets trigger. I am wondering if it is possible to do it once they leave the field as it going to confuse the user once they hit submit and nothing happens and then they have to go through the entire wizard again to find the validation errors.

It would be nice to inform them right away when they make a mistake.

I know that jquery wizard using the jquery form plugin that has validation but I rather try to stick with one method if possible to keep the code consistent.

Edit

This is what I have on the submit button

So I see a couple problems with this. First the validation only will really get triggered on submit. If it gets triggered and the validation error is on the second wizard step the user won't see it(unless some how can figure out way to find the first wrong validation error and switch them to that step).

The second problem I see is that they have to wait till the end to see if they have validation errors. I rather let them know asap.

Edit

It seems the wizard uses jquery validate but for some reason it is not working with mvc jquery validate way.

With more investigation I found that the wizard needs to see "required" as a class to trigger

I don't see in the documentation for jquery validate them ever using the class to validate the form. Is this something the person made up?

0 投票
1 回答
18950 浏览

c# - 修改模型时的 ASP.NET MVC TryValidateModel() 问题

我有一个两步表单过程,其中第一组数据存储在会话中。

...

控制器

TryValidateModel(modelVM)返回 false,表示需要 VideoClip 和 VideoThumnail,尽管它们从 seesionModel 映射到 viewModel。我添加了一个断点并检查它们不为空。

看起来有一些我不知道关于 ModelState 和 ValidateModel() 如何工作的基础功能,我只是不知道是什么。

更新

我不会说我已经解决了这个问题,但想出了一个不太好的解决方法,通过进入 ModelState 可以设置 ModelValue 使用SetModelValue()然后手动从模型状态中删除错误然后调用TryValidateModel()- 你可能甚至不必添加值只需删除我没有尝试过的错误。这是我的工作。

0 投票
2 回答
1152 浏览

asp.net-mvc-3 - ASP.NET MVC 3 - 绑定到 HTTP POST 时在 DTO 中设置空字段而不是失败

我有一个 HttpPost 控制器操作,它采用简单的形式 DTO 对象。

DTO 对象具有用于创建范围的可为空的日期时间字段。之所以设置为nullable,是因为模型绑定的表单是查询表单,用户不必在表单中输入日期值。

我遇到的问题是,如果用户输入的日期无效,我希望 MVC 默认模型绑定提供错误消息。如果我有一个需要 DateTime 的控制器操作,这会完美地发生吗?类型作为参数,但是因为我传递了一个包含 DateTime 的 DTO?键入模型绑定似乎只是设置了 DateTime?变量为空。这会导致意想不到的结果。

笔记:

如果不能绑定 DateTime,是否有告诉 MVC 模型绑定“失败”?为表单 DTO 对象赋值,而不仅仅是将其设置为 null?有没有更好的办法?将每个单独的表单输入传递给控制器​​是不可行的,因为 form/dto 对象中有大量属性(为了便于阅读,我排除了其中的许多属性)。

0 投票
3 回答
3237 浏览

model-view-controller - MVC 中的数据验证

假设我有一个“视图”用于填写租用 DVD 的表格,根据 MVC 架构,无论是“控制器”还是“模型”,谁应该验证表单数据?谢谢

0 投票
2 回答
1034 浏览

asp.net-mvc - MVC 3 验证不适用于复杂模型

我有一个 UserFormModel ,其中包含一个 UserModel ,它具有一组带有 [Required] 属性集的属性。我已经读过 MVC 3 开箱即用将默认验证模型中的模型。但是,当我在视图中提交一个空表单时,返回一个包含空 UserModel 的 UserFormModel,ModelState.IsValid 始终为真。

我尝试只将 UserModel 发送回我的控制器,并且验证正常。似乎只是当我使用它不验证的复杂模型时。

我还尝试使用 UserFormModel 中 User 属性的 [Required] 属性(我认为默认行为不需要该属性),但仍然没有进行验证。

对此的任何想法将不胜感激。