我刚读完http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-2-modelmetadata.html文章Brad列出了该Watermark属性,但没有提及哪个DataAnnotation用于填充它.
我一直在尝试使用单声道的asp.net mvc,数据注释抛出一个未实现的异常,是否有人知道Mono何时会支持这个?
根据这个页面http://www.go-mono.com/status/status.aspx?reference=4.0&profile=4.0&assembly=System.ComponentModel.DataAnnotations,它几乎完成了......
我在互联网上找到了一个RequiredIfAttribute,我将其修改为RequiredNotIf.该属性可以像这样使用.
[RequiredNotIf("LastName", null, ErrorMessage = "You must fill this.")]
public string FirstName { get; set; }
[RequiredNotIf("FirstName", null, ErrorMessage = "You must fill this")]
public string LastName { get; set; }
Run Code Online (Sandbox Code Playgroud)
和属性的源代码 ......
[AttributeUsageAttribute(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = true)]
public class RequiredNotIfAttribute : RequiredAttribute, IClientValidatable
{
private string OtherProperty { get; set; }
private object Condition { get; set; }
public RequiredNotIfAttribute(string otherProperty, object condition)
{
OtherProperty = otherProperty;
Condition = condition;
}
protected override ValidationResult IsValid(object …Run Code Online (Sandbox Code Playgroud) 我有以下问题我有这个由ajax调用的post动作:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult AX_Login(LoginVM usersubmitted)
{
if (ModelState.IsValid)
{
return Json(new { success = true });
}
else
{
return Json(new { success=false, error = true });
}
}
Run Code Online (Sandbox Code Playgroud)
另一方面,我有一个按钮的以下jquery处理程序:
$("#Jquery_LoginButton").click(
function () {
$.ajax({
type: "POST",
url: "@(Url.Action("AX_Login","Users"))",
data:$("#MiniLoginForm").serialize(),
success: function (result)
{
alert("Good");
},
error: function ()
{
alert("Bad");
}
});
}
)
Run Code Online (Sandbox Code Playgroud)
我的问题:已达到行动,但ajax呼叫始终以成功结束 - 即使模型无效 - .
问题:
你知道为什么会发生这种情况如果我设定为假成功吗?如果模型无效但我不会看到优雅的东西,我可以抛出异常.
您如何通过Data Annotations和Ajax正常管理验证?
我目前在一个字段上有以下数据注释
[StringLength(1000, MinimumLength = 6, ErrorMessage = "field must be atleast 6 characters")]
public string myField {get;set;}
Run Code Online (Sandbox Code Playgroud)
我是否可以更改数据注释,使其仅在用户在字段中键入内容时才有效?换句话说,可以将该字段留空,但如果用户在字段中键入值,则其长度应介于6-1000个字符之间.
我正在寻找一个正则表达式,我的在线研究带我到处都是.这是我要做的事情:
[Required]
[RegularExpression("MyRegExString", ErrorMessage = "Use only valid characters.")]
public string Name { get; set; }
Run Code Online (Sandbox Code Playgroud)
我想允许我的用户输入任何字母字符(大写或小写),数字,空格,只有_(下划线),$,#,*,(,),+,@,(逗号),和'(撇号).
谁能帮我生成这个字符串?
我基于这个问题,用asp.net mvc3和jquery validate创建自定义验证属性:
我已经更新它以使用一组字符串属性而不是bool.哪个工作正常.但是当我使用自定义ErrorMessage时,我的问题就出现了,不显示自定义消息.我无法弄清楚原因.
[RequiredOneFromGroup("PhoneGroup",
ErrorMessageResourceName = "PhoneGroup",
ErrorMessageResourceType = typeof(WizardStrings))]
public String Mobile { get; set; }
[RequiredOneFromGroup("PhoneGroup",
ErrorMessageResourceName = "PhoneGroup",
ErrorMessageResourceType = typeof(WizardStrings))]
public String Phone { get; set; }
Run Code Online (Sandbox Code Playgroud)
这是自定义验证属性:
[AttributeUsage(AttributeTargets.Property)]
public class RequiredOneFromGroup : ValidationAttribute, IClientValidatable
{
public RequiredOneFromGroup(string groupName)
{
ErrorMessage = string.Format("You must select at least one value from group \"{0}\"", groupName);
GroupName = groupName;
}
public string GroupName { get; private set; }
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{ …Run Code Online (Sandbox Code Playgroud) [Range(1900, DateTime.Now.Year, ErrorMessage = "Please enter a valid year")]
Run Code Online (Sandbox Code Playgroud)
这行不通。对于“ DateTime.Now.Year”,它告诉我
An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type
Run Code Online (Sandbox Code Playgroud) 任何回答此问题的人都知道,属性参数需要常量表达式.可选参数(对于任何事物,而不仅仅是属性)也需要常量表达式作为其默认值.
我遇到的(尽管是轻微的)不便是带有RegularExpressionAttribute模式参数.我的数据模型中有许多使用此属性的属性(可在System.ComponentModel.DataAnnotations中找到),每当我对验证模式进行更改时,我都必须返回并进行更改everryyywherrreee...这真的很烦人.
是否存在可以声明的.net结构,被识别为常量表达式,然后在通常需要常量表达式的情况下可用?
如果我可以在RegexPatternForNameProperty = "^[a-zA-Z0-9,.# ]{1,150}$"某个地方声明属性,然后根据需要更改那个值,那就太好了.
我正在尝试验证必须具有九位数代码且不能以四个零或四个九结尾且必须输入的不带特殊字符的属性。
我尝试了以下代码-
[RegularExpression(@"(^(?i:([a-z])(?!\1{2,}))*$)|(^[A-Ya-y1-8]*$)", ErrorMessage = "You can not have that")]
public string Test{ get; set; }
Run Code Online (Sandbox Code Playgroud)
但这不起作用。
例如: exasdea0000,asdea9999,exasde@0000或as_ea9999不能被输入。
我该如何实现?
data-annotations ×10
c# ×6
asp.net-mvc ×2
regex ×2
validation ×2
code-first ×1
jquery ×1
mono ×1
vb.net ×1