我正在使用DataAnnotationsutils 验证一个类.
我有一个Title有财产和Item财产的班级.我想对属性应用a RequiredAttribute,Title但只有当Item属性为null时它才应该无效; 如果Item使用对象设置属性,Title则不需要.
简而言之,我希望RequiredAttribute仅在满足类中的条件时才进行验证.
如何才能做到这一点.
由于我没有找到其他方式,并且因为我通常不经常需要这个功能,所以我决定使用类级验证器进行粗略的处理.我的问题是,有没有办法手动更新UI,使标题文本框具有红框,即使其无效?
更新2
我希望类级验证器在字段上进行汇总.例如,我必须填写Cost和SalesPrice字段,我想确保SalesPrice> Cost并使SalesPrice无效,否则,我不希望在类级别上出现全局验证错误.
我喜欢以xamly的方式做到这一点.
我有以下模型和视图,我非常希望以'dd/MM/yyyy'格式接受日期值.但是,尽管使用了DisplayFormat注释,我仍然会使用我选择的格式获得验证错误.
[MetadataType(typeof(MilestoneMetadata))]
public partial class Milestone {
public class MilestoneMetadata {
[Required][DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
public object Date { get; set; }
}
}
Run Code Online (Sandbox Code Playgroud)
和观点:
<div class="editor-field">
<%: Html.EditorFor(model => model.Date) %>
<%: Html.ValidationMessageFor(model => model.Date) %>
</div>
Run Code Online (Sandbox Code Playgroud)
命名空间等对于注释和主类在同一名称空间中是正确的.这不是我第一次遇到这个问题,但是我没有看到注释的结果,这些注释会影响表单值和模型之间的映射.日期模板对我没有帮助,因为我无法找到一种方法来设置在发布创建或更新时如何解析日期.
注意: 我不希望使用不同的UI文化来实现此目的.
我试图弄清楚如何使用MVC3 RC2和使用DataAnnotations修饰的模型格式化日期.
这是我的模特......
public class Model
{
[Display(Name = "Date of Birth")]
[DisplayFormat(@DataFormatString = "MM/dd/yyyy")]
public DateTime DateOfBirth { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这是我的控制器......
public class IndexController : Controller
{
public ActionResult Index()
{
return View( new Model() {DateOfBirth = DateTime.Now});
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的看法......
@model MvcApplication6.Models.Model
@Html.LabelFor(m=>m.DateOfBirth)
@Html.TextBoxFor(m => m.DateOfBirth)
@Html.EditorFor(m => m.DateOfBirth)
Run Code Online (Sandbox Code Playgroud)
当我运行该站点时,对于两个控件,页面显示一个文本框,其中包含时间字符串,当我想要的只是日期(由模型中的装饰属性指定).
我在这个模式中使用了Brad Wilson 关于ModelMetaData 的文章.
我做错了很明显吗?
asp.net-mvc datetime modelmetadata data-annotations asp.net-mvc-3
无论如何在MVC 3中使用DataAnnotations不允许在文本框中使用HTML?我看到了一种允许使用HTML(AllowHTMLAttribute)的方法,但是如果我不希望用户在文本框中键入任何HTML并想要警告他呢?
谢谢 :)
如何避免在ViewModel和业务/域对象上重复验证规则?
例如,我可以在我的ViewModel上使用DataAnnotation属性,这将在我的MVC Web应用程序中为我提供客户端和服务器端验证.但是这个ViewModel通常会被映射到一个业务/域对象并被赋予服务预先形成一些业务逻辑,这意味着验证必须再次发生,通常使用相同或类似的规则.无论如何围绕这个?
所以我是MVC3的新手,并使用DataAnnotations进行验证.一切都在很好的客户端,但我如何让服务器端版本工作?如果我禁用Javascript,则无法查看验证.
我的模型看起来像这样
[Required(ErrorMessageResourceName = "Verplicht", ErrorMessageResourceType = typeof (ValidatieStrings))]
[Display(Name="Voorletters", ResourceType = typeof (VeldNaamStrings))]
public string Voorletters { get; set; }
Run Code Online (Sandbox Code Playgroud)
我的控制器看起来像这样
using System.Web.Mvc;
using inschrijven_werknemer.Models;
namespace inschrijven_werknemer.Controllers
{
public class HomeController : LocalizationController
{
public ActionResult Index()
{
return View(new MedewerkInfoModel());
}
}
}
Run Code Online (Sandbox Code Playgroud)
我的视图看起来像这样
@model inschrijven_werknemer.Models.MedewerkInfoModel
<div class="stap-div" id="stap2">
@Html.EditorForModel("MedewerkInfoModel")
</div>
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
Name属性正常工作,但ShortName不起作用.
[Display(Name = "Date of the transfer the task", ShortName = "Trans date")]
public DateTime TransferDate { get; set; }
Run Code Online (Sandbox Code Playgroud)
即使我删除了Name属性,也会忽略ShortName(列标题中会显示"TransferDate").
在视图中我这样做:
@Html.DisplayNameFor(model => model.TransferDate)
Run Code Online (Sandbox Code Playgroud) namespace ClassValidation
{
public class Student
{
[Required(ErrorMessage = "Name is required")]
public String Firstname;
[Required(ErrorMessage = "Email is required")]
public String personalEmail;
}
}
private static void Main(string[] args)
{
Student student = new Student();
student.personalEmail = "del";
ValidationContext context = new ValidationContext(student, null, null);
List<ValidationResult> results = new List<ValidationResult>();
bool valid = Validator.TryValidateObject(student, context, results, true);
if (!valid)
{
foreach (ValidationResult vr in results)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.Write(" :: {0}{1}", vr.ErrorMessage, Environment.NewLine);
}
}
}
Run Code Online (Sandbox Code Playgroud) 我需要正则表达式验证,前5位数字是数字,然后是连字符,然后一位数字是字母,另一位是数字。
例如:23456-p5或12345-a3之类的东西。
我已经尝试过这样的事情
/^\d{5}-\d{2}$/
Run Code Online (Sandbox Code Playgroud)
在模型中我给出了这样的信息,因为我们不必在数据注释中给出^或$
[RegularExpression(@"d{5}-\d{2}")]
Run Code Online (Sandbox Code Playgroud)
但是我什至无法做到这一点。
我有一个Review Model,我试图验证该模型,以便当用户选择一个日期时,它不能是将来的日期。
Review.cs
public class Review : BaseEntity{
[Key]
public int Id {get; set;}
[Required(ErrorMessage="You need a restaurant name!")]
public string RestaurantName {get; set;}
[What do I put in here??]
public DateTime Date {get; set;}
}
Run Code Online (Sandbox Code Playgroud)
我是新手,文档很难理解。
非常感谢您的提前帮助。
data-annotations ×10
asp.net-mvc ×7
c# ×4
validation ×4
.net ×1
datetime ×1
html ×1
metadata ×1
razor ×1
regex ×1
server-side ×1
wpf ×1