是否可以在MVC 3中打开/关闭验证数据注释?(Is it possible to toggle Validation Data Annotations on/off in MVC 3?)

编程入门 行业动态 更新时间:2024-10-12 14:26:00
是否可以在MVC 3中打开/关闭验证数据注释?(Is it possible to toggle Validation Data Annotations on/off in MVC 3?)

我有两个单独的VIEWS访问相同的MODEL。 当我在模型上放置验证器数据注释时,它按照公布的方式工作,如果留空或不在范围内,则阻止提交数据(对于两个视图)。 但是,我有一个视图应该能够允许为属性保存空值或空值,而另一个视图需要输入或选择信息才能通过它。 换句话说,我想关闭一个视图中MODEL内属性的验证器,并将其保留为另一个视图。 这是示例代码:

模型:

[Range(1, 999, ErrorMessage = "A submittal is required")] public int SubmittalId { get; set; }

视图#1:

<label>@Model.AuditDoc.SubmittalsLabel.ConfigurableLabelDesc</label> @Html.ValidationMessageFor(x => x.AuditDoc.SubmittalId) @Html.DropDownListFor(x => x.AuditDoc.SubmittalId, new SelectList(Model.AuditDoc.ListOfSubmittals, "Id", "Name"))

视图#2:

<label>@Model.AuditDoc.SubmittalsLabel.ConfigurableLabelDesc</label> @Html.DropDownListFor(x => x.AuditDoc.SubmittalId, new SelectList(Model.AuditDoc.ListOfSubmittals, "Id", "Name"))

正如您所看到的,我想为View#2禁用该验证器数据注释,并将其保留为View#1。

I have two separate VIEWS accessing the same MODEL. When I put the validator data annotations on the model, it works as advertised and prevents the data from being submitted (for both views) if left blank or not within range. However, I have one view that should be able to allow empty or null values to be saved for a property whereas another view needs to require information to be entered or selected before it lets it through. In other words, I'd like to turn off the validator on the property within the MODEL for one view and leave it on for the other view. Here's the example code:

MODEL:

[Range(1, 999, ErrorMessage = "A submittal is required")] public int SubmittalId { get; set; }

VIEW #1:

<label>@Model.AuditDoc.SubmittalsLabel.ConfigurableLabelDesc</label> @Html.ValidationMessageFor(x => x.AuditDoc.SubmittalId) @Html.DropDownListFor(x => x.AuditDoc.SubmittalId, new SelectList(Model.AuditDoc.ListOfSubmittals, "Id", "Name"))

VIEW #2:

<label>@Model.AuditDoc.SubmittalsLabel.ConfigurableLabelDesc</label> @Html.DropDownListFor(x => x.AuditDoc.SubmittalId, new SelectList(Model.AuditDoc.ListOfSubmittals, "Id", "Name"))

As you can see, I would like to disable that validator data annotation for View #2 and leave it on for View #1.

最满意答案

这不可能通过默认的数据注释集来实现。但是,您可以选择使用2个单独的视图模型或编写自己的validationAttribute。

我写了一次..虽然我厌恶使用它..

public class RequiredOnAttribute : ValidationAttribute { public string[] URLs { get; set; } public override bool IsValid(object value) { if (URLs.Contains(System.Web.HttpContext.Current.Request.Url.AbsolutePath)) { if (string.IsNullOrEmpty(value as string)) { return false; } } return true; } }

用法:

[RequiredOn(URLs = new string[] { "/create", "/edit" })] public string MyModelField { get; set; }

你可以为Range,RegEx等做同样的事情。

This isn't possible via the default set of data annotations.. however, you have the choice of using 2 separate view models or writing your own validationAttribute.

I wrote this once.. although I loathed using it..

public class RequiredOnAttribute : ValidationAttribute { public string[] URLs { get; set; } public override bool IsValid(object value) { if (URLs.Contains(System.Web.HttpContext.Current.Request.Url.AbsolutePath)) { if (string.IsNullOrEmpty(value as string)) { return false; } } return true; } }

Usage:

[RequiredOn(URLs = new string[] { "/create", "/edit" })] public string MyModelField { get; set; }

You could do the same for Range, RegEx, etc..

更多推荐

视图,MODEL,view,AuditDoc,电脑培训,计算机培训,IT培训"/> <meta name="descri

本文发布于:2023-08-07 15:02:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464531.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:注释   数据   MVC   toggle   Annotations

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!