Generate N pairs of checkboxes and N textboxes I've written a piece of code, where a pair of checkboxes and a textbox are generated N amount of times. Depending on what's in the DB, the checkbox/textbox may have a default value or are left empty. 

I've pasted the code below, could anyone take a look to see if I haven't created an abomination?

&lt;td&gt;
    @if (singleItem != null &amp;&amp; singleItem.FolderRead)
    {
        &lt;input type="checkbox" id="canRead_@item.SecureFolderID" class="CHKreader" onchange="changePermissions(@item.SecureFolderID)" checked /&gt;
    }
    else
    {
        &lt;input type="checkbox" id="canRead_@item.SecureFolderID" class="CHKreader" onchange="changePermissions(@item.SecureFolderID)" /&gt;
    }
&lt;/td&gt;
&lt;td&gt;
    @if (singleItem != null &amp;&amp; singleItem.FolderWrite)
    {
        &lt;input type="checkbox" id="canWrite_@item.SecureFolderID" class="CHKwriter" onchange="changePermissions(@item.SecureFolderID)" checked /&gt;
    }
    else
    {
        &lt;input type="checkbox" id="canWrite_@item.SecureFolderID" class="CHKwriter" onchange="changePermissions(@item.SecureFolderID)" /&gt;
    }
&lt;/td&gt;

&lt;td&gt;
    @if (singleItem != null &amp;&amp; singleItem.ExpireDate != null)
    {
        &lt;input class="datepick" id="expDate-@item.SecureFolderID" onchange="changePermissions(@item.SecureFolderID)" value="@singleItem.ExpireDate.Value.ToString("yyyy-MM-dd")" /&gt;
    }
    else
    {
        &lt;input class="datepick" id="expDate-@item.SecureFolderID" onchange="changePermissions(@item.SecureFolderID)" /&gt;
    }
&lt;/td&gt;


I know the copy/paste code is not that clean. The textbox could be filled in with a variabele, which could remain empty if the DB record is as well. But I don't know if that makes the code more 'readable' or if such a method works for the Checkbox as well.
I would use the razor boolean attributes:

&lt;td&gt;
  &lt;input type="checkbox" id="canRead_@item.SecureFolderID" class="CHKreader" onchange="changePermissions(@item.SecureFolderID)" checked="@(singleItem != null &amp;&amp; singleItem.FolderRead)" /&gt;
&lt;/td&gt;
&lt;td&gt;
    &lt;input type="checkbox" id="canWrite_@item.SecureFolderID" class="CHKwriter" onchange="changePermissions(@item.SecureFolderID)" checked="@(singleItem != null &amp;&amp; singleItem.FolderWrite)" /&gt;
&lt;/td&gt;
&lt;td&gt;
    &lt;input class="datepick" id="expDate_@item.SecureFolderID" onchange="changePermissions(@item.SecureFolderID)" value="@((singleItem != null &amp;&amp; singleItem.ExpireDate != null)?singleItem.ExpireDate.Value.ToString("yyyy-MM-dd"):null)" /&gt;
&lt;/td&gt;


Going further, if item/singleitem are coming from an Enumerable, there is probably a better way to generate the Ids as well.  Especially if you want them to post back into a nice object.  Remove the classes, remove the onchange handler.

@for(var i=0;i&lt;Model.Count;i++)
{
  &lt;tr data-something="@item.SecureFolderID"&gt;
    &lt;td&gt;
      @Html.CheckBoxFor(m=&gt;m[i].FolderRead)
    &lt;/td&gt;
    &lt;td&gt;
      @Html.CheckBoxFor(m=&gt;m[i].FolderWrite)
    &lt;/td&gt;
    &lt;td&gt;
      @Html.TextBoxFor(m=&gt;m[i].ExpireDate,new{type="date"}) // HTML5 date w/datepicker for browsers that support it
    &lt;/td&gt;
  &lt;/tr&gt;
}

&lt;style&gt;
 #mytable td:first-child input { CHKreader styles here };
 #mytable td:nth-child(2) input { CHKwriter styles here };
 #mytable td:nth-child(3) input { datepick styles here };
/* Optionally use these instead
 input[name*="FolderRead"] { CHKreader styles here };
 input[name*="FolderWrite"] { CHKwriter styles here };
 input[name*="ExpireDate"] { datepick styles here };
*/
&lt;/style&gt;
&lt;script&gt;
  $('#mytable').on("change","input",function(){
    var folderid=$(this).closest("tr").data("something");
    changePermissions(folderid);
  });
&lt;/script&gt;


You could also use an EditorFor on the date and create your own template if you prefer that route.