Reputation: 5670
I've found a property of my helper that I think will give me access to the properties of my model, but I was hoping to get an instance of the model itself. I have a view with a strongly typed Model. One property of the model, is a collection of other models (TestModel). I would like to render each of the items in the collection in a standard way. So, my view code would look something like this.
@foreach(var testModel in @Model.Items){
@Html.DisplayViewerFor(@testModel)
}
My helper looks something like this.
public static MvcHtmlString DisplayViewerFor(this HtmlHelper<TestModel> helper, Expression<Func<TestModel>> expression, bool rightAligned = true) {
var modelData = helper.ViewData;
var prop = modelData[""];
var outterDiv = new TagBuilder("div");
outterDiv.AddCssClass(rightAligned ? "item-display-right" : "item-display");
//Create other markup using modelData here
//Would prefer to use an instance of TestModel
return new MvcHtmlString(outterDiv.ToString(TagRenderMode.EndTag));
}
Upvotes: 1
Views: 158
Reputation: 887315
It sounds like you want a value, not an expression.
Extend the non-generic HtmlHelper
class and take a raw TestModel
instance as a parameter.
You only need an expression tree if you want to find out the property name.
Upvotes: 1