so after a few weeks, i have got my head around asp.net mvc and have converted two webforms sites over. In addition to just the port, did some refactoring to have clean seperate of model, controller and view code.
One pattern that i see that i think still needs improvement is the following and i would like advice or feedback on these items as i am not sure how bad these smells are.
Problem: HTML rendering knowledge in your controller - its very easy to have your business logic generate a bunch of HTML and shove it in ViewData and then have your view be a bunch of code that looks like this:
`<%= Html.Encode(ViewData["Title"]) %>
`<%= Html.Encode(ViewData["Content"]) %>
`<%= Html.Encode(ViewData["Footer"]) %>
What is the best solution for this? I obviously want to avoid this logic in my view? Should i shove the data down to the client and have this "rendering" logic in HTMLHelper classes
I end up with code like this in the controller class that builds up HTML tables . .
Stringbuilder gridBuilder = new StringBuilder();
while (index < objDS.Count)
{
// start of table row
gridBuilder.AppendLine("<tr align=\"center\">");
for (int column = 0; column < numberOfColumns; column++)
{
if (index < objDS.Count)
{
int record = (index) + ((objDS.CurrentPageIndex) * 12);
DataRow dr = photosDataSet.Tables[0].Rows[record];
gridBuilder.AppendLine("<td width=\"187.5\" valign=\"top\">");
int pictureNumber = Convert.ToInt32(dr.ItemArray[0].ToString());
string picnum = pictureNumber.ToString().PadLeft(3, '0');
int picNumberlink = pictureNumber - 1;
string image = "/pics/" + AlbumName + "/Thumbnails/" + AlbumName + "-pic" + picnum + ".jpg";
gridBuilder.AppendLine("<a id=\"picLinks_" + record + "\" class=\"picLinks\" href=''><img class=\"instant ishadow50\" src=\"" + image + "\"></img></a>");
gridBuilder.AppendLine("<br/>");
gridBuilder.AppendLine(dr.ItemArray[1].ToString());
gridBuilder.AppendLine("</td>");
}
index++;
}
gridBuilder.AppendLine("</tr>");
}
gridBuilder.AppendLine("</table>");
ViewData["Content"] = gridBuilder.ToString();