我正在使用Entity Framework的Code First功能,我正在试图弄清楚如何指定在自动生成数据库时应该创建的列数据类型.
我有一个简单的模型:
public class Article
{
public int ArticleID { get; set; }
public string Title { get; set; }
public string Author { get; set; }
public string Summary { get; set; }
public string SummaryHtml { get; set; }
public string Body { get; set; }
public string BodyHtml { get; set; }
public string Slug { get; set; }
public DateTime Published { get; set; }
public DateTime Updated { get; set; }
public virtual …
Run Code Online (Sandbox Code Playgroud) c# entity-framework sql-server-ce data-annotations ef-code-first
在我的模型中,我的一个属性上有以下DataAnnotations
[Required(ErrorMessage = "*")]
[DisplayFormat(DataFormatString = "{0:d}")]
[DataType(DataType.Date)]
public DateTime Birthdate { get; set; }
Run Code Online (Sandbox Code Playgroud)
所需的注释效果很好,我添加了另外两个来尝试删除时间.它使用绑定到视图中的输入
<%=Html.TextBoxFor(m => m.Birthdate, new { @class = "middle-input" })%>
Run Code Online (Sandbox Code Playgroud)
但是,每当视图加载时,我仍然会在输入框中显示时间.无论如何使用DataAnnotations删除它?
我有一个这样的视图模型:
public class SignUpViewModel
{
[Required(ErrorMessage = "Bitte lesen und akzeptieren Sie die AGB.")]
[DisplayName("Ich habe die AGB gelesen und akzeptiere diese.")]
public bool AgreesWithTerms { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
视图标记代码:
<%= Html.CheckBoxFor(m => m.AgreesWithTerms) %>
<%= Html.LabelFor(m => m.AgreesWithTerms)%>
Run Code Online (Sandbox Code Playgroud)
结果:
没有执行验证.到目前为止,这没关系,因为bool是一个值类型,永远不会为null.但即使我使AgreesWithTerms可以为空,它也无法工作,因为编译器大喊大叫
"模板只能用于字段访问,属性访问,单维数组索引或单参数自定义索引器表达式."
那么,处理这个问题的正确方法是什么?
是否可以使用[Range]
日期注释?
就像是
[Range(typeof(DateTime), DateTime.MinValue.ToString(), DateTime.Today.ToString())]
Run Code Online (Sandbox Code Playgroud) 当使用正则表达式中的特殊字符时,DataAnnotations验证器无法在asp.net mvc 4 razor视图中工作.
模型:
[StringLength(100)]
[Display(Description = "First Name")]
[RegularExpression("^([a-zA-Z0-9 .&'-]+)$", ErrorMessage = "Invalid First Name")]
public string FirstName { get; set; }
Run Code Online (Sandbox Code Playgroud)
剃刀查看:
@Html.TextBoxFor(model => Model.FirstName, new { })
@Html.ValidationMessageFor(model => Model.FirstName)
Run Code Online (Sandbox Code Playgroud)
不显眼的验证在视图中呈现为:
<input type="text" value="" tabindex="1" style="height:auto;" name="FirstName" maxlength="100" id="FirstName" data-val-regex-pattern="^([a-zA-Z0-9 .&amp;&#39;-]+)$" data-val-regex="Invalid First Name" data-val-length-max="100" data-val-length="The field FirstName must be a string with a maximum length of 100." data-val="true" class="textfield ui-input-text ui-body-d ui-corner-all ui-shadow-inset valid">
Run Code Online (Sandbox Code Playgroud)
上面html中的正则表达式模式没有按照模型的RegularExpression中的指定进行渲染,即使输入有效数据(Sam's
)也会导致错误.
我怎么处理这个?
--UPDATE--
我根据@Rick建议更新了代码
[StringLength(100)]
[Display(Description = "First Name")] …
Run Code Online (Sandbox Code Playgroud) 从现在开始,我使用了优秀的FluentValidation 库来验证我的模型类.在Web应用程序中,我将它与jquery.validate插件结合使用,以执行客户端验证.一个缺点是许多验证逻辑在客户端重复,不再集中在一个地方.
出于这个原因,我正在寻找替代方案.有许多例子出有表示数据注解的使用来执行模型验证.看起来很有希望.我无法找到的一件事是如何验证依赖于另一个属性值的属性.
我们以下面的模型为例:
public class Event
{
[Required]
public DateTime? StartDate { get; set; }
[Required]
public DateTime? EndDate { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我想确保EndDate
大于StartDate
.我可以编写一个扩展ValidationAttribute的自定义验证属性,以便执行自定义验证逻辑.不幸的是我找不到获取模型实例的方法:
public class CustomValidationAttribute : ValidationAttribute
{
public override bool IsValid(object value)
{
// value represents the property value on which this attribute is applied
// but how to obtain the object instance to which this property belongs?
return true;
}
} …
Run Code Online (Sandbox Code Playgroud) 任何人都可以解释一下MVC中UIHint属性的用途.我们为什么需要这个.以及何时以及如何使用.谢谢
想要创建自定义数据注释验证.有关如何创建它们的有用指南/示例吗?
首先:
具有最小和最大长度的StringLength.我知道.NET 4可以做到这一点,但是想在.NET 3.5中做同样的事情,如果可能的话,只能定义最小长度(至少x个字符),最大长度(最多x个字符),或者两者都是(在x和y之间).
其次:
使用模数运算验证 - 如果数字是有效长度,我希望使用模数11算法进行验证(我已经在JavaScript中实现了它,所以我想它只是一个简单的移植?)
更新:
解决了第二个问题,只是复制JavaScript实现并进行一些调整,所以不需要解决方案.
我有以下MVC 5 Razor HTML帮助器:
@Html.TextBoxFor(m => m.ShortName,
new { @class = "form-control", @placeholder = "short name"})
Run Code Online (Sandbox Code Playgroud)
我需要这个字段是必需的(例如,当用户导航而没有放置值值时,会有一个红色轮廓).在WebForms HTML 5中我可以说<input type="text" required />
具有这种效果.在Razor语法中实现此功能的正确语法是什么?
鉴于以下对象,
public class Question
{
[Required]
public string QuestionText { get; set; }
[Range(1, 5)]
public int Difficulty { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
使用以下验证码
ICollection<ValidationResult> results = new List<ValidationResult>();
Question question = new Question();
ValidationContext ctx = new ValidationContext(question, null, null);
Validator.TryValidateObject(question, ctx, results);
// results.Length = 1
Run Code Online (Sandbox Code Playgroud)
为什么Range属性在Required(值显然为0)时不会创建验证错误?
data-annotations ×10
asp.net-mvc ×7
c# ×2
razor ×2
validation ×2
.net ×1
.net-3.5 ×1
asp.net ×1
checkbox ×1
date ×1
html-helper ×1
regex ×1