FluentValidation 库相对于 .NET System.ComponentModel.DataAnnotations 有什么优势?
它是否提供更灵活的验证,因为它没有在属性上进行注释(静态字段验证),例如根据另一个属性值验证属性?
我的代码:
启动.cs
public void ConfigureServices(IServiceCollection services)
{
...
services.AddLocalization(options => options.ResourcesPath = "Resources");
services.AddMvc()
.AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
.AddDataAnnotationsLocalization(options =>
{
options.DataAnnotationLocalizerProvider = (type, factory) =>
factory.Create(typeof(SharedResource));
});
...
}
Run Code Online (Sandbox Code Playgroud)
共享资源.cs
namespace MyProj.Classes
{
/// <summary>
/// Dummy class to group shared resources
/// </summary>
public class SharedResource
{
}
}
Run Code Online (Sandbox Code Playgroud)
FooViewModel.cs
public class FooViewModel
{
[Required(ErrorMessage = "EmailRequired")]
[EmailAddress(ErrorMessage = "EmailIsNotValid")]
[Display(Name = "Email")]
[DataType(DataType.EmailAddress)]
public string Email { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
FooPage.cshtml
...
<input asp-for="Email" class="form-control">
<div …Run Code Online (Sandbox Code Playgroud) 我正在创建 asp.net mvc 应用程序,其中将错误消息存储在数据库中,现在我想在用户单击提交时使用必需的字段注释而不是自定义 js 验证,我的模型如下所示,
[Required(ErrorMessage = "static error message")]
public string AttributeValue { get; set; }
Run Code Online (Sandbox Code Playgroud)
我想添加动态错误消息而不是 -> (静态错误消息)
有没有办法从控制器添加动态错误消息。
这是我第一次探索 DataAnnotations(我希望能够流畅地使用它)...并且我不明白为什么以下代码会引发编译时错误:
CS0592 - 属性“索引”在此声明类型上无效。它仅对“类”声明有效
public class Holiday
{
[Key]
public int Id { get; set; }
[Required]
[Index(IsUnique = true)]
public DateTime? Date { get; set; }
public string Name { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我的目标是使Date列唯一...我认为[Index(IsUnique = true)]在列上使用是使其唯一的正确方法...但它不允许我在列上使用 Index 属性,只能在类上使用...
请教我如何实现这一目标?
我在Linq-to-Sql中有模型类,其中部分类标有数据注释属性和对xVal的引用.
当我将视图直接绑定到模型时,一切都很好,xVal生成的JS和服务器端双重检查.
我的许多视图都没有输入到一个特定的模型,所以我正在设置视图模型类.我没有公开整个模型实例,而是将属性暴露给我允许/需要由视图设置的模型.
// foo model
public class Foo {
public string FooField { ... }
public Bar Bar { ... }
}
// bar model, where bar is a parent relationship of foo in the db
public class Bar {
public string BarField { ... }
}
// view model stuff
public class FooViewModel {
private Foo foo;
public FooViewModel() {
foo = new Foo() { Bar = new Bar() };
}
public Foo Model {
get { return …Run Code Online (Sandbox Code Playgroud) 我在C#4中使用tha命名空间System.ComponentModel.DataAnnotations来实现我自己的验证属性,它看起来像这样
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
public sealed class MyCustomValidator : ValidationAttribute {
private String Property1 { get; set; }
private String Property2 { get; set; }
public ValeTaxiSituacaoRequired(String property1, String property2) {
Property1 = property1;
Property2 = property2;
}
public override bool IsValid(object value) {
//validation logic
}
}
Run Code Online (Sandbox Code Playgroud)
我想用这个属性如下
[MyCustomValidator("Name", "Job")]
[MyCustomValidator("Name", "Email")]
[MyCustomValidator("Name", "Job")]
public class Employe {
}
Run Code Online (Sandbox Code Playgroud)
问题是只进行了一次验证.如何执行所有验证(使用asp.net mvc 2)?
c# attributes fluentvalidation data-annotations asp.net-mvc-2
在我的MVC2 AdminArea中,我想为每个域模型创建一个概述表.我正在使用如下所示的DataAnnotations来获取这些域模型对象的属性:
[DisplayName("MyPropertyName")]
public string Name { get; set; }
Run Code Online (Sandbox Code Playgroud)
现在我的问题是:如果我的视图收到我的域模型的集合,我如何访问DisplayName属性?我需要这个来构建在通常之外定义的表头
<% foreach (var item in Model) { %>
Run Code Online (Sandbox Code Playgroud)
环.在这个循环中,我可以写
<%: Html.LabelFor(c => item.Name) %>
Run Code Online (Sandbox Code Playgroud)
但有没有办法使用项目集合而不是具体实例来访问此信息?
提前致谢!
如何显示自定义错误消息
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:d}")]
Run Code Online (Sandbox Code Playgroud)
我的模型中的代码如下,
[Display(Name = "When was that document issued?")]
[DataType(DataType.Date, ErrorMessageResourceName = "DocumentIssueDate_DataType",ErrorMessageResourceType = typeof(CustomErrorMessages))]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:d}")]
public DateTime? DocumentIssueDate { get; set; }
Run Code Online (Sandbox Code Playgroud)
在我的视图中,当我在日期文本框中输入201时,我收到以下错误消息.如何修改以下错误消息.
值"201"对DocumentIssueDate无效.
我正在尝试创建一个新的Orchard CMS模块,以模仿我构建的另一个MVC项目。我正在将VS 2013 Express与最新的Orchard v1.72一起使用。我有一个包含using语句的类
使用System.ComponentModel.DataAnnotations;
我有以下属性
[MaxLength(16)]
public virtual string Symbol { get; set; }
[StringLength(16)]
public virtual string SymbolSequence { get; set; }
[StringLength(512)]
public virtual string Notes { get; set; }
public virtual double Begin { get; set; }
public virtual double End { get; set; }
[Range(0.01, int.MaxValue, ErrorMessage = "Please enter a value 0.01 or larger.")]
public virtual double Increment { get; set; }
Run Code Online (Sandbox Code Playgroud)
是否有任何原因在StringLength和Range注释同时找不到MaxLength注释?
它们都驻留在相同的System.ComponentModel.DataAnnotations命名空间中,并且在其他项目中没有问题。
我该如何解决?
.NET 4.5的Edit Retargeting似乎已经解决了问题
使用.Net Core MVC.我需要设置我的模型,以便EntityFrameworkCore将自动生成ID属性.我认为只需添加[Key]注释即可,但事实并非如此.事实上,当ID字段留空时会产生错误,这总是因为我将其隐藏在用户之外.
我需要它来代替每次为模型自动生成一个唯一的ID.我该怎么做呢?我的ID属性目前是类型int.
ExampleModel
public class ExampleModel
{
[Key]
public int ID { get; set; }
public string SomeData { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
ExampleView
@model MyNamespace.Models.ExampleModel
<form asp-action="Create" asp-controller="ExampleModel">
@Html.EditorForModel()
<input type="submit" name="Submit" value="Create"/>
</form>
Run Code Online (Sandbox Code Playgroud)
ExampleController
public IActionResult Create ()
{
return View();
}
public IActionResult Create (ExampleModel model)
{
if (ModelState.IsValid)
{
_context.ExampleModels.Add(model);
_context.SaveChanges();
return RedirectToAction("Index");
}
return View(model);
}
Run Code Online (Sandbox Code Playgroud) c# data-annotations entity-framework-core asp.net-core-mvc asp.net-core
data-annotations ×10
c# ×5
asp.net-mvc ×4
asp.net-core ×2
validation ×2
attributes ×1
localization ×1
orchardcms ×1
viewmodel ×1
views ×1