è necessario essere l'uso:
Html.DisplayFor(model => model.DeviceComponentName, new { @id = "DeviceComponentName"})
Per id dinamica e altre proprietà, io uso :
Classe per i metadati:
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;
}
classe per il provider di metadati:
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;
}
}
Add aiutante:
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");
}
}
fornitore di registro in Global.ASax:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
//....
ModelMetadataProviders.Current = new MetadataProvider();
}
nel modello u possibile utilizzare AdditionHtml come come:
[AdditionalHtml(Id = "OrderNo", CssClass = ShortTextStyle, Disabled = true)]
public string OrderNo { get; set; }
e ora è possibile utilizzare sintassi per js (in vista):
$('#@Model.GetHtmlId(x => x.PropertyName)')
E in vista, è possibile utilizzare:
@Html.DisplayFor(x => x.FormDate)
Attributi HTML a tached automaticamente
Aggiunta la sintassi del non-rasoio - il codice stesso rimane lo stesso. – naspinski
La sintassi del non-rasoio dovrebbe avere il carattere @ prima dell'Html? –
hai ragione Sean: P modificato – naspinski