我想在我的域模型中添加验证器属性(在ASP.NET MVC应用程序中),我正在尝试在2个框架,验证应用程序块和DataAnnotations之间做出决定.他们似乎做了类似的任务,所以我想选择将来最受支持/使用的任务.似乎DataAnnotations是更新的(并且在3.5 SP1中构建到框架中),那么人们认为验证应用程序块将变得过时吗?
[标记为社区维基,因为这是主观的]
我在一个纯C#应用程序的项目中使用DataAnnotations,根据DataAnnotations属性验证模型/文档的最佳方法是什么?
以及DisplayName例如.
[DisplayName("Address line 1 ")]
public string Address1{get; set;}
Html.LabelFor(model => model.Address1)
Run Code Online (Sandbox Code Playgroud)
我需要显示工具提示,例如.
[DisplayName("Address line 1 ")]
[ToolTip("The first line of your address as it appears on you bank statement")]
public string Address1{get; set;}
Html.LabelFor(model => model.Address1)
Html.ToolTipFor(model => model.Address1)
Run Code Online (Sandbox Code Playgroud)
我可以扩展DisplayName DataAnnotation来执行此操作吗?我看不出它是如何完成的.
谢谢!
是否有基于集合的属性的数据注释验证规则?
我有以下内容
<DisplayName("Category")>
<Range(1, Integer.MaxValue, ErrorMessage:="Please select a category")>
Property CategoryId As Integer
<DisplayName("Technical Services")>
Property TechnicalServices As List(Of Integer)
Run Code Online (Sandbox Code Playgroud)
我正在寻找一个验证器,我可以添加到TechnicalServices属性来设置集合大小的最小值.
我已经安装了Scott的Kirkland DataAnnotationsExtensions.
在我的模型中,我有:
[Numeric]
public double expectedcost { get; set; }
Run Code Online (Sandbox Code Playgroud)
在我看来:
@Html.EditorFor(model => model.expectedcost)
Run Code Online (Sandbox Code Playgroud)
现在,当页面尝试渲染时,我收到以下错误:
不显眼的客户端验证规则中的验证类型名称必须是唯一的.以下验证类型不止一次出现:数字
任何想法为什么我收到错误?
asp.net-mvc unobtrusive-javascript data-annotations asp.net-mvc-3
我有下面的模型(用户),我用它来添加新用户和更新现有用户.当我添加新用户时,需要输入用户名和密码,当我更新时,只需要输入用户名,因为不允许更改密码.这是问题,添加一个新用户一切正常,因为我输入了名称和密码值,因此ModelState.IsValid返回true,但是当更新用户时没有输入密码,所以它总是具有null值是什么使得ModelState.IsValid始终返回false.有没有办法使用相同的模型,即使在添加视图中需要密码,在更新视图中它不是?拜托,有什么建议吗?
public class User {
public int ID { get; set; }
[Display(Name = "Nome do Usuário")]
[Required(ErrorMessage = "Digite o Nome do Usuário.")]
public string name { get; set; }
[Display(Name = "Senha")]
[Required(ErrorMessage = "Digite a Senha.")]
public string password { get; set; }
}
public ActionResult Add()
{
return View();
}
[HttpPost]
public ActionResult Add(User user)
{
UsuariosViewModel viewModel = new UsuariosViewModel();
if (ModelState.IsValid)
{
viewModel.Msg = new AdmUsuarios().CadastraUsuario(user);
}
return View(viewModel);
}
public ActionResult Update(int id) …Run Code Online (Sandbox Code Playgroud) 我想从我的模型中的一个属性传递一个值到我的数据注释来验证我的密码属性,但我不知道如何实现这一点.当我这样做时,我收到以下错误:
an attribute argument must be a constant expression typeof expression or array
Run Code Online (Sandbox Code Playgroud)
我的模特:
public class LoginModel
{
public string Voornaam { get; set; }
public string Achternaam { get; set; }
public string Gebruikersnaam { get; set; }
[Password(AttributeVoornaam = this.Voornaam, AttributeAchternaam = this.Achternaam, AttributeGebruikersnaam = this.Gebruikersnaam)]
public string Wachtwoord { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
在我的数据注释中,我这样做:
public class PasswordAttribute : ValidationAttribute
{
public string AttributeVoornaam { get; set; }
public string AttributeAchternaam { get; set; }
public string AttributeGebruikersnaam …Run Code Online (Sandbox Code Playgroud) 我在论坛上搜索了很多,但是没有找到关于这个问题的任何内容.
我在EntityFramework Code First中有2个属性:
[Column(TypeName = "Money")]
public decimal? Debit { get; set; }
[Column(TypeName = "Money")]
public decimal? Credit { get; set; }
Run Code Online (Sandbox Code Playgroud)
其中一个不应为null,但另一个应为null示例:
Debit=null;
Credit=34;
Debit=45;
Credit=null;
Run Code Online (Sandbox Code Playgroud)
另一方面,不应该将它们都设置为空或者不设置它们.是否可以使用数据注释处理此问题,还是应该通过解决方法解决它?
最好的祝福!
null entity-framework properties code-first data-annotations
我正在使用带有ASP.NET MVC助手的Kendo UI Grid和自动生成的列.
[DefaultValue(60 * 60)]我的视图模型中有注释,但剑道助手似乎并不尊重这一点.
我是否可以指定默认值(可能包含数据注释)而无需手动描述列?
asp.net-mvc data-annotations kendo-ui kendo-grid kendo-asp.net-mvc
我的ViewModel中有以下字段:
[DataType(DataType.Date)]
[Display(Name = "Preferred date)")]
public DateTime EventDate { get; set; }
Run Code Online (Sandbox Code Playgroud)
产生以下标记:
<input class="input-validation-error form-control text-box single-line" data-val="true" data-val-date="The field Preferred date must be a date." data-val-required="The Preferred date field is required." id="EventDate" name="EventDate" type="date" value="">
Run Code Online (Sandbox Code Playgroud)
如果用户没有选择值(即,将此字段留空value=""),则返回到ViewModel的数据为"{1/1/0001 12:00:00 AM}"并且ModelState.IsValid是false.
我不希望这个字段是必需的!
我试着用我的替换标记:
<input class="form-control text-box single-line" id="EventDate" name="EventDate" type="date" value="1/1/1980">
Run Code Online (Sandbox Code Playgroud)
但是,返回的值仍然是"{1/1/0001 12:00:00 AM}"并且ModelState.IsValid是false.
如何在此字段上禁用必需的验证(仅限)?提前致谢.
data-annotations ×10
asp.net-mvc ×6
c# ×2
model ×2
validation ×2
asp.net ×1
code-first ×1
kendo-grid ×1
kendo-ui ×1
modelstate ×1
null ×1
properties ×1
vb.net ×1