You can write a strongly typed helper with a Ξ»-expression:
public static class StatusExtensions { public static IHtmlString StatusBox<TModel, TProperty>( this HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> ex ) { return new HtmlString( "Some things here ... " + helper.HiddenFor(ex)); } }
and then:
@Html.StatusBox(model => model.RowInfo.Created)
UPDATE:
As requested in the comments section here is a revised version of the helper:
public static class StatusExtensions { public static IHtmlString StatusBox<TModel>( this HtmlHelper<TModel> helper, Expression<Func<TModel, RowInfo>> ex ) { var createdEx = Expression.Lambda<Func<TModel, DateTime>>( Expression.Property(ex.Body, "Created"), ex.Parameters ); var modifiedEx = Expression.Lambda<Func<TModel, DateTime>>( Expression.Property(ex.Body, "Modified"), ex.Parameters ); return new HtmlString( "Some things here ..." + helper.HiddenFor(createdEx) + helper.HiddenFor(modifiedEx) ); } }
and then:
@Html.StatusBox(model => model.RowInfo)
Needless to say, custom HTML helpers should be used to create small parts of HTML. Complexity can grow quickly, in which case I would recommend that you use an editor template for the RowInfo type.
source share