Reputation: 17946
I would like to get the MvcHtmlString
results from the HtmlHelper.Display
method of a type that is different than my current HtmlHelper
. The code below compiles but always returns an empty string.
public static MvcHtmlString GetPropertyValue(this HtmlHelper html, string property, object value)
{
var dictionary = new ViewDataDictionary();
dictionary.Add(property, value);
var fooHelper = new HtmlHelper<FooModel>(
new ViewContext(
html.ViewContext,
new WebFormView(html.ViewContext, "dummy"),
dictionary,
new TempDataDictionary(),
TextWriter.Null),
new ViewPage());
return fooHelper.Display(property);
}
All the helper objects (ViewContexts
, ControllerContexts
, ViewDataDictionaries
, etc.) are kind of a black box to me; I'm sure I'm missing something obvious.
How do I get this to work?
For the curious: this is to be used on an audit page in my application. I have the property name (as a string
), old value, and new value from the database. I also know the type that would be used to display an actual instance of the model represented by the record. I'd like to reuse all the data annotations from my model class to get the HTML to display.
Upvotes: 1
Views: 476
Reputation: 7125
I had kinda the same Question. What I wanted:
I wanted to create my own html extension method which would do some stuff and than call the DisplayFor. In that display for method I would pass my different model object.
My solution
public static MvcHtmlString ShowProperty<T>(this HtmlHelper<T> helper, NewModel mynewModel){
return helper.DisplayFor(m => myNewModel, "TemplateName");
}
Mayby this would work for some one
Upvotes: 1