あなたは使用する必要があります:
Html.DisplayFor(model => model.DeviceComponentName, new { @id = "DeviceComponentName"})
動的 ID およびその他のプロパティについては、次を使用します。
メタデータのクラス:
public class AdditionalHtml : Attribute, IMetadataAware
{
public string Id { get; set; }
public string Type { get; set; }
public string CssClass { get; set; }
public string PlaceHolder { get; set; }
public string Style { get; set; }
public string OnChange { get; set; }
public int Rows { get; set; }
public int MaxLength { get; set; }
public bool ReadOnly { get; set; }
public bool Disabled { get; set; }
public Dictionary<string, object> OptionalAttributes ()
{
var options = new Dictionary<string, object>();
if ( !string.IsNullOrWhiteSpace( Id ) )
options.Add( "id", Id );
if ( !string.IsNullOrWhiteSpace( Type ) )
options.Add( "type", Type );
if ( !string.IsNullOrWhiteSpace( CssClass ) )
options.Add( "class", CssClass );
if ( !string.IsNullOrWhiteSpace( PlaceHolder ) )
options.Add( "placeholder", PlaceHolder );
if ( !string.IsNullOrWhiteSpace( OnChange ) )
options.Add( "onchange", OnChange );
if ( !string.IsNullOrWhiteSpace( Style ) )
options.Add( "style", Style );
if ( Rows != 0 )
options.Add( "rows", Rows );
if ( MaxLength != 0 )
options.Add( "maxlength", MaxLength );
if ( ReadOnly )
options.Add( "readonly", "readonly" );
if ( Disabled )
options.Add( "disabled", "disabled" );
return options;
}
メタデータ プロバイダーのクラス:
public class MetadataProvider : DataAnnotationsModelMetadataProvider
{
protected override ModelMetadata CreateMetadata ( IEnumerable<Attribute> attributes, Type containerType, Func<object> modelAccessor, Type modelType, string propertyName )
{
var metadata = base.CreateMetadata( attributes, containerType, modelAccessor, modelType, propertyName );
var additionalHtmlValues = attributes.OfType<AdditionalHtml>().FirstOrDefault();
if ( additionalHtmlValues != null )
{
metadata.AdditionalValues.Add( "AdditionalHtml", additionalHtmlValues );
}
return metadata;
}
}
ヘルパーを追加:
public static class HtmlAttributesHelper
{
public static string GetHtmlAttribute<T> ( this T model, Expression<Func<T, object>> expression, string attribName )
{
string strDefault = String.Empty;
MemberInfo member = null;
switch ( expression.Body.NodeType )
{
case ExpressionType.Lambda:
case ExpressionType.Convert:
{
var body = expression.Body as UnaryExpression;
if ( body == null )
return strDefault;
var operand = body.Operand as MemberExpression;
if ( operand == null )
return strDefault;
member = operand.Member;
break;
}
case ExpressionType.MemberAccess:
{
var body = expression.Body as MemberExpression;
if ( body == null )
return strDefault;
member = body.Member;
break;
}
default:
{
return expression.Body.ToString() + " " + expression.Body.NodeType.ToString();
}
}
if ( member == null )
return strDefault;
var attr = member.GetCustomAttributes( typeof( AdditionalHtml ), false );
if ( attr.Length > 0 )
{
return ( attr [ 0 ] as AdditionalHtml ).OptionalAttributes() [ attribName.ToLower() ].ToString();
}
// Return Name of Property if AdditionalHtml.Id is empty
return attribName == "Id" ? member.Name : strDefault;
}
public static string GetHtmlId<T> ( this T model, Expression<Func<T, object>> expression )
{
return model.GetHtmlAttribute( expression, "Id" );
}
}
Global.asax にプロバイダーを登録します。
protected void Application_Start ()
{
AreaRegistration.RegisterAllAreas();
//....
ModelMetadataProviders.Current = new MetadataProvider();
}
モデルでは、次のように AdditionHtml を使用できます。
[AdditionalHtml( Id = "OrderNo", CssClass = ShortTextStyle, Disabled = true )]
public string OrderNo { get; set; }
これで、js に sintax を使用できるようになりました (表示中):
$('#@Model.GetHtmlId( x => x.PropertyName)')
ビューでは、次を使用できます。
@Html.DisplayFor( x => x.FormDate )
自動的に添付される HTML 属性