Custom template in razor view
<p>I am trying to apply some custom templating to a razor view. It is a bit special so here is an example.</p>

<p>Lets say out model contains a Person object with Firstname and Lastname.</p>

<p>The template would look like this witch I would write in the cshtml file</p>

<pre><code>&lt;div data-template-id="testTemplate" data-template-model="@Model.Person"&gt;
&lt;span&gt;{{Firstname}}&lt;/span&gt;
&lt;span&gt;{{Lastname}}&lt;/span&gt;
&lt;/div&gt;
</code></pre>

<p>I would then make a HtmlHelper method that populates the template with data from Model.Person</p>

<p>So har I have got the access to the data but I am no sure how to access the view and get the template.</p>

<pre><code>public static MvcHtmlString Test&lt;TModel, TValue&gt;(this HtmlHelper&lt;TModel&gt; html, Expression&lt;Func&lt;TModel, TValue&gt;&gt; expression){
    var model = html.ViewData.Model;
        string propName = "";
        if (expression.Body is MemberExpression)
            propName = ((MemberExpression)expression.Body).Member.Name;
        var value = model.GetType().GetProperty(propName).GetValue(model, null);
        // TODO: get template and insert data from model
        return new MvcHtmlString($"&lt;div&gt;{value}&lt;/div&gt;");
}
</code></pre>

<p>I know it is a bit strange why I would do this. But the short answer is that in some conditions I need to return some completly different html, based on some internal conditions. In which case I would just remove the template completely. </p>