我目前正在使用MVC 1.0和.NET 3.5.我正在使用DataAnnotations来验证我的模型.我正在尝试添加使用RegularExpression来验证邮政编码.当我尝试以下操作时,我已将我的正则表达式存储在资源文件中,因为许多模型将使用它
[RegularExpression(Resources.RegexPostcode, ErrorMessage="Postcode format invalid")]
public string Postcode { get; set; }
Run Code Online (Sandbox Code Playgroud)
我构建时遇到以下错误:
属性参数必须是属性参数类型的常量表达式,typeof表达式或数组创建表达式.
有没有办法使用资源文件中的值作为正则表达式,还是需要在每个具有邮政编码的模型中输入实际的正则表达式字符串?
谢谢
谁能告诉我如何在ASP.NET MVC 2中使用DataType.Custom?
我有一个Entity Framework生成的类,具有以下属性:
public DateTime LaunchDate;
public DateTime ExpirationDate;
Run Code Online (Sandbox Code Playgroud)
我需要强制执行ExpirationDate> LaunchDate.
我正在使用各种帖子中描述的好友类.我将自定义验证属性(在属性上)应用于同一伙伴类中的其他属性,这些属性正在运行.
因为我需要比较两个属性,我直接在类上使用属性(AttributeTargets.Class)
这是我的自定义验证属性:
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)]
public class PropertyMustBeGreaterThanAttribute : ValidationAttribute
{
private const string _defaultErrorMessage = "'{0}' must be greater than '{1}'!";
public PropertyMustBeGreaterThanAttribute(string property, string greaterThan)
: base(_defaultErrorMessage)
{
GreaterThan = greaterThan;
Property = property;
}
public string Property { get; private set; }
public string GreaterThan { get; private set; }
public override string FormatErrorMessage(string name)
{
return String.Format(CultureInfo.CurrentUICulture, ErrorMessageString,
GreaterThan, Property);
} …Run Code Online (Sandbox Code Playgroud) 在我看来,我的验证变得很奇怪.
我的模特有这个属性.
[Display(Name = "Overflow Capacity")]
[RegularExpression(@"[-+]?[0-9]*\.?[0-9]?[0-9]", ErrorMessage = "Number required.")]
[Range(0,9999.99,ErrorMessage = "Value must be between 0 - 9,999.99")]
public decimal OverFlowCapacity { get; set; }
Run Code Online (Sandbox Code Playgroud)
我的观点是这样的:
<tr>
<td>@Html.LabelFor(m=> m.OverFlowCapacity)</td>
<td>@Html.EditorFor(m=>m.OverFlowCapacity)</td>
<td> @Html.ValidationMessageFor(model => model.OverFlowCapacity)</td>
</tr>
Run Code Online (Sandbox Code Playgroud)
如果我输入类似'ABC'的值,我会收到验证消息'Number required'如果我输入值999999,我会收到验证消息'Value必须介于0 - 9,999.99'之间
当我按预期关闭文本框时,会收到这两条消息.当我将文本框值保留为空并关闭时,我没有按预期得到任何错误.
但是,当我提交时,我收到验证消息"需要溢出容量字段".
我不知道这是从哪里来的.我已尝试从模型中删除所有验证属性,但仍然会收到"必需"消息.我不知所措.
这是我引用的脚本.
我还有mvcfoolproof的其他问题,我可以稍后发布.我想知道这是不是以某种方式对我的问题负责.
我的DomainRegistry模型具有以下属性:
[Domain("Extension")]
public string Name { get; set; }
[Required(ErrorMessage = "Select extension")]
public string Extension { get; set; }
Run Code Online (Sandbox Code Playgroud)
域是我的自定义数据注释,我已经IsValid尝试了方法中的所有内容以访问扩展属性内的值。
我的自定义数据注释中包含以下内容:
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
public class DomainAttribute : ValidationAttribute
{
public string ExtensionProperty { get; set; }
public DominioAttribute(string property)
{
ExtensionProperty = property;
}
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(value);
var extension = (string) properties.Find(Propriedade, true).GetValue(value);
if (extension == null) return new …Run Code Online (Sandbox Code Playgroud) 我班上有一个属性,我会选择.只有probem是该属性使用Guid并且是标准所必需的.我不知道如何使它可以为空.
我想要完成的是将membershipprovider扩展为一个新表,Groups,并在Group和Users之间建立一对多的链接.
财产:
public virtual Guid GroupId { get; set; }
public virtual Group Group { get; set; }
Run Code Online (Sandbox Code Playgroud)
那么我怎样才能使Guid属性成为可空?
我有一个包含没有[Required]属性的布尔值的模型
public bool IsOptedIn { get; set; }
Run Code Online (Sandbox Code Playgroud)
我已经覆盖Object.cshtml如下,并@Html.EditorForModel()用于生成我的表单
@{
var properties = ViewData.ModelMetadata.Properties
.Where(prop => prop.ShowForEdit && !ViewData.TemplateInfo.Visited(prop));
}
@foreach (var prop in properties)
{
var hasModelStateError = ViewContext.ViewData.ModelState.Any(m => m.Key == prop.PropertyName)
&& ViewContext.ViewData.ModelState[prop.PropertyName].Errors != null
&& ViewContext.ViewData.ModelState[prop.PropertyName].Errors.Count > 0;
<div class="control-group
@(hasModelStateError ? "error" : string.Empty)
@prop.PropertyName.ToLower()">
@if (prop.IsReadOnly)
{
<b>@prop.GetDisplayName()</b>
@Html.Display(prop.PropertyName)
}
else if (prop.HideSurroundingHtml)
{
@Html.Editor(prop.PropertyName)
}
else
{
<label class="control-label">
@prop.GetDisplayName()
@if (prop.IsRequired)
{
<span class="required">*</span>
}
</label> …Run Code Online (Sandbox Code Playgroud) 使用[RegularExpression(@"^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$")]属性永远不会验证我的模型的电子邮件属性.我是正则表达式的新手,所以我无法弄清楚问题是什么.帮助不大?
我正在尝试为我的mvc应用程序创建简单的正则表达式验证。我试过了:
[Required]
[RegularExpression("(\\d\\d\\/\\d\\d\\/\\d\\d\\d\\d)")]
Run Code Online (Sandbox Code Playgroud)
而且它不起作用。我想强制客户以2015年1月1日的格式输入日期
我花了很多时间来找出答案,但我没有。当我发布模型而未选择任何选项(实际上是选择0 - Select-option)时,Required验证不起作用。
我还尝试从服务代码中删除以编程方式添加的默认选项,将其从查看代码中删除,验证也没有像这样工作。
如果我完全删除了默认选项,则视图引擎会自动从列表中选择第一个选项,并且永远不会完成验证。
如何使服务器端验证正确完成?
这是我的模特
public class AuditViewModel
{
public Guid Id { get; set; }
[Display(Name = "Subject")]
[Required(ErrorMessage = "IsRequired")]
public string Subject { get; set; }
public string AuditType { get; set; }
public string LocationCountry { get; set; }
public string LocationOffice { get; set; }
[Required(ErrorMessage = "IsRequired")]
[Display(Name = "AuditType")]
public int AuditTypeId { get; set; }
public string CreatedOn { get; set; }
public …Run Code Online (Sandbox Code Playgroud) data-annotations ×10
asp.net-mvc ×4
c# ×2
regex ×2
validation ×2
.net ×1
asp.net ×1
class ×1
guid ×1
razor ×1
resources ×1
select ×1
types ×1