I have created a display template in ~/Views/Shared/DisplayTemplates
named ImpactMatrix.cshtml
. It accepts a nullable int and renders a two-dimensional matrix with the selected number highlighted:
@model int?
@{
var matrix = ImpactMatrix.GetMatrix();
}
<div class="impactmatrix">
<table>
@for (int i = 0; i < matrix.GetLength(0); i++)
{
<tr>
@for (int j = 0; j < matrix.GetLength(1); j++)
{
var cell = matrix[i, j];
<td data-color="@cell.Color"
class="matrix @(Model == cell.Value ? cell.Color.ToString() : "")">
@cell.Value
</td>
}
</tr>
}
</table>
</div>
It's easily reusable and works great. I can invoke it within my view like so:
@Html.DisplayFor(m=> m.ImpactFactor, "ImpactMatrix")
Now I've decided to extend that and make it an editor as well. The idea is to add a hidden input for the selected number and wrap the input along with the matrix template with a div. From there it should be a simple matter to use Javascript to interact with my display grid and populate the hidden input.
I've created an editor template, also named ImpactMatrix.cshtml
, within my ~/Views/Shared/EditorTemplates
folder. Here's the code:
@model int?
<div class="impactmatrix-editor">
@Html.HiddenFor(m => m)
@Html.DisplayFor(m => m, "ImpactMatrix")
</div>
My problem is that the hidden input renders correctly, but the nested display template does not render inside my editor template. Is what I am trying to do possible?
It seems that it is not currently supported.
However, I have found a solution using the Html.Partial
in this article: Nested @Html.DisplayFor(model => baseClass, "BaseClass") for base class template not rendering
Rewrite you editor template like this:
@model int?
<div class="impactmatrix-editor">
@Html.HiddenFor(m => m)
@Html.Partial("~/Views/Shared/DisplayTemplates/ImpactMatrix.cshtml", Model)
</div>
Note: @Paul Hadfield commented on this issue in the article I have mentioned above, that this issue has been fixed for ASP MVC 4. But even though I run this version on my PC, I was not able to make nested templates working.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With