Note that there are some explanatory texts on larger screens.

plurals
  1. POAvoid duplicate formatting between server-side Razor view and client-side jQuery template
    primarykey
    data
    text
    <p>I have a search results page where I output a list of items formatted in a particular way using a MVC Razor view. </p> <pre><code>@for (int i = 0; i &lt; group.Count(); i++) { &lt;div class="result"&gt; &lt;div class="ordinal"&gt;@((i+1).ToString()).&lt;/div&gt; &lt;div class="detail"&gt;&lt;p&gt;@group.ElementAt(i).Name&lt;/p&gt;&lt;/div&gt; &lt;/div&gt; } </code></pre> <p>The client can further filter those results using jQuery AJAX to retrive a new dataset as JSON and jQuery templates to render the resultset in place of the original. This is the jQuery Template:</p> <pre><code>&lt;script id="resultTemplate" type="text/x-jquery-tmpl"&gt; {{each(i, result) results}} &lt;div class="result"&gt; &lt;div class="ordinal"&gt;${i+1}.&lt;/div&gt; &lt;div class="detail"&gt;&lt;p&gt;${name}&lt;/p&gt;&lt;/div&gt; &lt;/div&gt; {{/each}} &lt;/script&gt; </code></pre> <p>which is bound after AJAX call:</p> <pre><code>var result = $("#resultTemplate").tmpl({ results: data }); $("#resultsColumn").empty().append(result); </code></pre> <p>Note how I had to duplicate the HTML formatting for a search result in both server-side Razor code and client-side jQuery code. I'd like to have just one version of the data bound template to reduce the chance of mismatches when I have to make changes.</p> <p>Reading <a href="http://stephenwalther.com/blog/archive/2010/11/30/an-introduction-to-jquery-templates.aspx" rel="nofollow">Stephen Walter's Intro to jQuery Templates</a> he's hinting at "better together" integration when using jQuery templates with ASP.NET MVC so I was wondering if there's a facility that addresses the above scenario. </p> <p>Thanks.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload