Contrary to accepted and popular answer by @Andrew Hare. I would rather implement the markup generation only in javascript (and reuse that for building HTML tables) than implement markup only in C# (if javascript needs that functionality too, of course).
Because:
- Javascript is is better than C# for HTML, because it can modify the already rendered DOM. So with Javascript you can do much more than just static HTML. And maybe you don't need it now, but nowadays are not rare the requirements that aim for the application becoming more like a Rich Internet Application.
- AJAX calls are more likely to be slower than the worst Javascript implementation which handles server-side data and transforms that into new rows of an existing table. And also slower than making the table from scratch in Javascript.
- Less server requests (and more client side requirements, but that usually is not a problem).
- There a several very good Javascript frameworks that makes really nice tables (and much more).
I know that ASP.NET MVC has a lot of nice examples on how to use HTML Helpers for your controls, resulting in clean and fast development. But if you end up creating Javascript functions anyway then I think you should consider again the separation of concerns, and stick with Javascript and discard the duplication in the HTML Helpers.
Because expanding your Javascript functions to create the whole table if you are already creating the rows should not be that hard I guess. Here is an idea of how to pass the data to Javascript and then using the function "createRow" that you already have in Javascript:
<script>
var data = [];
<% foreach (var item in Model) { %>
data.push({
Id: <%= Html.Encode(item.Id) %>
, Title: <%= Html.Encode(item.Title) %>
});
<% } %>
createTableHeader();
for (var i = 0; i < data.length; i++) {
createRow(data[i]);
}
createTableFooter();
</script>