Suppose I have the following partial view for Decimal located at "~/Views/Shared/EditorTemplates/Decimal.cshtml":
@model decimal?
@{
var attrs = ViewData;
if (ViewData["type"] == null)
{
attrs.Add("type", "number");
}
}
@Html.TextBoxFor(m => m, attrs)
I want to also use this template for properties of type Int32. So I create the following at "~/Views/Shared/EditorTemplates/Int32.cshtml":
@model int?
@Html.Partial("~/Views/Shared/EditorTemplates/Decimal.cshtml", (decimal?)Model)
Is there a better way to reuse editor templates? Are there any consequences to this pattern that I may have missed?
(Edit: added explicit cast from int to decimal?.)
You can define an EditorTemplate as a whole. Once defining for both data types. For instance, you can use the following code samples:
Definition of EditorTemplate :
Uassage of Helper in View :
definition in ViewModel Class is
OR