我有两个单独的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
发布评论