To be honest, I have no idea what to call this or how to start to search it.
I have a display page with a standard layout.
<div>
<label for="field">Field Name:</label>
@Model.Field
</div>
While trying to make this more change friendly, I want to make a template instead of typing out each field with the above code.
I created a partial view with the following:
@model System.Object
<div>
@Html.LabelFor(m => m)
@Html.DisplayFor(m => m)
</div>
On my view, I added the following:
@Html.Partial("_BillField", Model.Field)
The model then has a description with like:
public ModelName {
[Display(Name="Field Description")]
public decimal Field { get; set; }
}
This works when on the main view, but the label is missing when using the template. What am I missing?
Update: Per @K. Bob I make the change to the partial view:
<div>
@Html.LabelFor(m => m)
@Html.DisplayFor(m => m)
</div>
Update 2: For clarity of what I want.
In the end, I want to be able to do:
@Html.Partial("_BillField", Model.Field1)
@Html.Partial("_BillField", Model.Field2)
@Html.Partial("_BillField", Model.Field3)
And have the equivalent of:
<div>
<label for="field1">Field 1 Name:</label>
@Model.Field1
</div>
<div>
<label for="field2">Field 2 Name:</label>
@Model.Field2
</div>
<div>
<label for="field3">Field 3 Name:</label>
@Model.Field3
</div>
Sorry for not making that clearer.
LabelForet al is not aFunc, but anExpression<Func>. This is because it can use that to access the model member (that is, its value, type, name, and attributes, e.g.DisplayName) via reflection. When you pass the model member directly through to the partial, you're changing that expression, so it can no longer access the member in situ, and can only access the type and value of the member, because it lacks the context of the model.