[英]C# ExpressiveAnnotations how to add Asterisk notation when field is requied
很好的庫: https : //github.com/jwaliszko/ExpressiveAnnotations
但是我不知道在需要該字段時如何添加星號表示法。 這可能嗎。 如果是,怎么辦?
碼
[RequiredIf("IsCustomerInputRequired == true")]
[Display(Name = Translations.Global.USER)]
public string CustomerInput { get; set; }
我嘗試:
var metaData = ModelMetadata.FromLambdaExpression(expression, html.ViewData);
var isRequired = metaData.ContainerType.GetProperty(metaData.PropertyName).GetCustomAttributes(typeof(RequiredIfAttribute), false).Any();
但始終是true
什么是錯的
您可以在屬性上使用DataAnnotations以獲得稍微不同的所需效果:
using System.ComponentModel.DataAnnotations;
[Required(ErrorMessage="[Education] Please enter the University of Institution.")]
public string School
{
get { return _school; }
set { _school = value; OnPropertyChanged();}
}
或者,您可以嘗試自己的HtmlHelper擴展程序:
public static string RequiredMarkFor<TModel, TValue>(this HtmlHelper html, Expression<Func<TModel, TValue>> expression)
{
if(ModelMetadata.FromLambdaExpression(expression, html.ViewData).IsRequired)
return "*";
else
return string.Empty;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.