views:

88

answers:

3

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.

  1. 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();
+1  A: 

In my opinion, HTML Encoding should be done in the view, because it's specific to HTML. If you move this encoding up to the controller, you remove the major advantage of using views: separating the formatting logic from the business logic.

R. Bemrose
my issue is that i have code in the controller that knows about HTML. i will update the question
ooo
+1  A: 

For rendering HTML tables I would recommend you the Grid component from MvcContrib.

Darin Dimitrov
+2  A: 

The answere is - you souldn't have html in your controller. If you can't use the grid from MvcContrib as suggested then you sould create a class that takes the DataTable and outputs the html - in an overridden ToString method or a specific method that you call .

And use it like :

<%= new MyGridGenerator(DataTable ..) %>
sirrocco