I am trying to write my first customer Html Helper extension method following the format
public static MvcHtmlString<TModel, TProperty>
MyHelperFor(this HtmlHelper<TModel> helper,
Expression<Func<TModel, TProperty>> expression)
And there seem to be several different ways to access the property name and value from the expression
var body = expression.Body as MemberExpression;
var propertyName = body.Member.Name;
var propertyInfo = typeof(TModel).getProperty(propertyName)
var propertyValue = propertyInfo.GetValue(helper.ViewData.Model);
and
var metadata = ModelMetadata.FromLambdaExpression(expression, html.ViewData);
var propertyName = metadata.PropertyName;
var propertyValue = metadata.Model;
and also
TModel model = (TModel)helper.ViewContext.ViewData.ModelMetadata.Model;
TProperty value = expression.Compile().Invoke(model);
Can somebody explain the difference between these methods? Are there any situations where one is superior to the others?
Aucun commentaire:
Enregistrer un commentaire