I have a bunch of code in my old Web Forms app I'm migrating to MVC. There are sections of various pages that need to have HTML generated by the view and then regenerated by an AJAX call. A lot of this HTML is pretty complex, so it really helps to keep all of the html generation in one spot (preferrably a template).
My question is, is there a better approach to this? Should I be using partial views or something? How would that translate into the WebService call? Is there something more efficient I could be doing?
Basically this is what I'm doing now (this is a really basic, crappy, example so I apologize):
In Pseudo-code:
The Markup
<!--- some html here -->
<div id="myContent">
<%=StaticMethods.GetContent()%>
</div>
<button id="btnUpdate">Update</button>
<!--- some html here -->
The AJAX Javascripty stuff
$(document).ready(function() {
$('#btnUpdate').click(function() {
$.ajax({
url:'MyService.asmx/MyWebServiceMethod',
/*crap here*/
success: function(result, status, code) {
$('#myContent').html(result);
}
});
});
});
The Static Method
public static class StaticMethods {
public static string GetContent() {
var sb = new StringBuilder();
var somethings = GetSomeRandomThings();
//just an example. it's normally much more complex.
foreach(var something in somethings) {
sb.AppendFormat("<li>{0}</li>",something.Name);
}
}
}
MyWebService
public string MyWebServiceMethod() {
return StaticMethods.GetContent();
}