Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

any ideas for avoiding duplicate code in C# and javascript

i have an asp.net mvc website where i build up most of the page using C# for example building up html tables given a set of data from my viewmodel

i also have a lot of javascript that then dynamcially modifies these tables (add row for example).

the javascript code to add a new row looks extremely similar to my "rendering" code i have in C# that is used to build up the html table in the first place.

Every time i change the c# code to a add a new field, i have to remember to go back to the javascript code to do the same.

is there a better way here?

like image 578
leora Avatar asked Jan 18 '10 16:01

leora


1 Answers

One way to do this is to expose the logic that generates the markup as a web service on your server and have JavaScript get that markup via an AJAX call rather than duplicating the logic.

This way you can call something like a CreateRow method from JavaScript that will actually invoke the same exact logic that you used on the server when you rendered the page.

like image 182
Andrew Hare Avatar answered Oct 20 '22 11:10

Andrew Hare