2015-10-23 19 views
0

很好庫: https://github.com/jwaliszko/ExpressiveAnnotationsC#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什麼是錯的

回答

0

您可以使用您的屬性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; 
} 
+0

THX的暗示,但RequiredMarkFor方法總是在我的例子假。和我使用ExpressiveAnnotations庫不是簡單的必填字段 – mbrc