Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>you must be use:</p> <pre><code>Html.DisplayFor(model =&gt; model.DeviceComponentName, new { @id = "DeviceComponentName"}) </code></pre> <p>For dynamic id and other properties, i use:</p> <p>Class for metadata:</p> <pre><code>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&lt;string, object&gt; OptionalAttributes () { var options = new Dictionary&lt;string, object&gt;(); 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; } </code></pre> <p>Class for metadata provider:</p> <pre><code>public class MetadataProvider : DataAnnotationsModelMetadataProvider { protected override ModelMetadata CreateMetadata ( IEnumerable&lt;Attribute&gt; attributes, Type containerType, Func&lt;object&gt; modelAccessor, Type modelType, string propertyName ) { var metadata = base.CreateMetadata( attributes, containerType, modelAccessor, modelType, propertyName ); var additionalHtmlValues = attributes.OfType&lt;AdditionalHtml&gt;().FirstOrDefault(); if ( additionalHtmlValues != null ) { metadata.AdditionalValues.Add( "AdditionalHtml", additionalHtmlValues ); } return metadata; } } </code></pre> <p>Add helper:</p> <pre><code>public static class HtmlAttributesHelper { public static string GetHtmlAttribute&lt;T&gt; ( this T model, Expression&lt;Func&lt;T, object&gt;&gt; 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 &gt; 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&lt;T&gt; ( this T model, Expression&lt;Func&lt;T, object&gt;&gt; expression ) { return model.GetHtmlAttribute( expression, "Id" ); } } </code></pre> <p>Register provider in Global.asax:</p> <pre><code>protected void Application_Start () { AreaRegistration.RegisterAllAreas(); //.... ModelMetadataProviders.Current = new MetadataProvider(); } </code></pre> <p>In your model u can use AdditionHtml like as:</p> <pre><code>[AdditionalHtml( Id = "OrderNo", CssClass = ShortTextStyle, Disabled = true )] public string OrderNo { get; set; } </code></pre> <p>And now you can use sintax for js (in view): </p> <pre><code>$('#@Model.GetHtmlId( x =&gt; x.PropertyName)') </code></pre> <p>And in view, you can use:</p> <pre><code>@Html.DisplayFor( x =&gt; x.FormDate ) </code></pre> <p>Html attributes attached automatically</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload