1

I have a base class that a number of classes inherit from. Each derived class has a partial view inclusing the base class. What I'd like to do is something like this:

@Html.DisplayFor(item => ((Inq_DetailsBase)Model))

To display all the headers and values from the base class. However i get a

Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions.

I understand that I need to then type out the member names for that base class. However, this seems to defeat the purpose of re-usability. Is there a way in my partial views to display the members in my base class without having to type each of them out?

1
  • Use Html.Partial() instead of Html.DisplayFor.
    – ataravati
    Nov 5, 2015 at 17:01

1 Answer 1

2

Is there a reason you can't just put the Base into it's own Partial? Something similar to the answer to this question.

View (~/Views/Home/Inq_Details.cshtml):

@model Inq_Details
@using (Html.BeginForm())
{
    @Html.Partial("Inq_DetailsBase", Model)
    @Html.TextBoxFor(m => m.DetailsField1)
    @Html.TextBoxFor(m => m.DetailsField2)
    ....
}

Partial (~/Views/Home/Inq_DetailsBase.cshtml):

@model Inq_DetailsBase
@Html.TextBoxFor(x => x.BaseField1)
@Html.TextBoxFor(x => x.BaseField2)
@Html.TextBoxFor(x => x.BaseField3)

Then you just need to change the Detail pages and the Base will sta

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.