Note that there are some explanatory texts on larger screens.

plurals
  1. POam i using asp.net mvc correctly?
    primarykey
    data
    text
    <p>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. </p> <p>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.</p> <ol> <li><p>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:</p> <p>`&lt;%= Html.Encode(ViewData["Title"]) %></p> <p>`&lt;%= Html.Encode(ViewData["Content"]) %></p> <p>`&lt;%= Html.Encode(ViewData["Footer"]) %></p></li> </ol> <p>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</p> <p>I end up with code like this in the controller class that builds up HTML tables . .</p> <pre><code>Stringbuilder gridBuilder = new StringBuilder(); while (index &lt; objDS.Count) { // start of table row gridBuilder.AppendLine("&lt;tr align=\"center\"&gt;"); for (int column = 0; column &lt; numberOfColumns; column++) { if (index &lt; objDS.Count) { int record = (index) + ((objDS.CurrentPageIndex) * 12); DataRow dr = photosDataSet.Tables[0].Rows[record]; gridBuilder.AppendLine("&lt;td width=\"187.5\" valign=\"top\"&gt;"); 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("&lt;a id=\"picLinks_" + record + "\" class=\"picLinks\" href=''&gt;&lt;img class=\"instant ishadow50\" src=\"" + image + "\"&gt;&lt;/img&gt;&lt;/a&gt;"); gridBuilder.AppendLine("&lt;br/&gt;"); gridBuilder.AppendLine(dr.ItemArray[1].ToString()); gridBuilder.AppendLine("&lt;/td&gt;"); } index++; } gridBuilder.AppendLine("&lt;/tr&gt;"); } gridBuilder.AppendLine("&lt;/table&gt;"); ViewData["Content"] = gridBuilder.ToString(); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload