I want to be able to display some text, but also have the text be modifiable via jQuery.
<%= Html.DisplayFor(model => model.DeviceComponentName)%>
you must be use:
Html.DisplayFor(model => model.DeviceComponentName, new { @id = "DeviceComponentName"})
For dynamic id and other properties, i use:
Class for metadata:
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 OptionalAttributes ()
{
var options = new Dictionary();
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;
}
Class for metadata provider:
public class MetadataProvider : DataAnnotationsModelMetadataProvider
{
protected override ModelMetadata CreateMetadata ( IEnumerable attributes, Type containerType, Func
Add helper:
public static class HtmlAttributesHelper
{
public static string GetHtmlAttribute ( this T model, Expression> 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 ( this T model, Expression> expression )
{
return model.GetHtmlAttribute( expression, "Id" );
}
}
Register provider in Global.asax:
protected void Application_Start ()
{
AreaRegistration.RegisterAllAreas();
//....
ModelMetadataProviders.Current = new MetadataProvider();
}
In your model u can use AdditionHtml like as:
[AdditionalHtml( Id = "OrderNo", CssClass = ShortTextStyle, Disabled = true )]
public string OrderNo { get; set; }
And now you can use sintax for js (in view):
$('#@Model.GetHtmlId( x => x.PropertyName)')
And in view, you can use:
@Html.DisplayFor( x => x.FormDate )
Html attributes attached automatically