4
\$\begingroup\$

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?

<td>
 @if (singleItem != null && singleItem.FolderRead)
 {
 <input type="checkbox" id="[email protected]" class="CHKreader" onchange="changePermissions(@item.SecureFolderID)" checked />
 }
 else
 {
 <input type="checkbox" id="[email protected]" class="CHKreader" onchange="changePermissions(@item.SecureFolderID)" />
 }
</td>
<td>
 @if (singleItem != null && singleItem.FolderWrite)
 {
 <input type="checkbox" id="[email protected]" class="CHKwriter" onchange="changePermissions(@item.SecureFolderID)" checked />
 }
 else
 {
 <input type="checkbox" id="[email protected]" class="CHKwriter" onchange="changePermissions(@item.SecureFolderID)" />
 }
</td>
<td>
 @if (singleItem != null && singleItem.ExpireDate != null)
 {
 <input class="datepick" id="[email protected]" onchange="changePermissions(@item.SecureFolderID)" value="@singleItem.ExpireDate.Value.ToString("yyyy-MM-dd")" />
 }
 else
 {
 <input class="datepick" id="[email protected]" onchange="changePermissions(@item.SecureFolderID)" />
 }
</td>

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.

Nick Udell
5,2471 gold badge29 silver badges68 bronze badges
asked May 28, 2015 at 10:02
\$\endgroup\$
4
  • \$\begingroup\$ What are singleItem and item? \$\endgroup\$ Commented May 28, 2015 at 13:20
  • \$\begingroup\$ custom class objects, defined above the wall of code you see above. \$\endgroup\$ Commented May 28, 2015 at 14:24
  • \$\begingroup\$ So neither are loop variables? \$\endgroup\$ Commented May 28, 2015 at 14:35
  • \$\begingroup\$ Both of them are, truth be told. Both of them change every iteration \$\endgroup\$ Commented May 28, 2015 at 15:02

1 Answer 1

2
\$\begingroup\$

I would use the razor boolean attributes:

<td>
 <input type="checkbox" id="[email protected]" class="CHKreader" onchange="changePermissions(@item.SecureFolderID)" checked="@(singleItem != null && singleItem.FolderRead)" />
</td>
<td>
 <input type="checkbox" id="[email protected]" class="CHKwriter" onchange="changePermissions(@item.SecureFolderID)" checked="@(singleItem != null && singleItem.FolderWrite)" />
</td>
<td>
 <input class="datepick" id="[email protected]" onchange="changePermissions(@item.SecureFolderID)" value="@((singleItem != null && singleItem.ExpireDate != null)?singleItem.ExpireDate.Value.ToString("yyyy-MM-dd"):null)" />
</td>

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<Model.Count;i++)
{
 <tr data-something="@item.SecureFolderID">
 <td>
 @Html.CheckBoxFor(m=>m[i].FolderRead)
 </td>
 <td>
 @Html.CheckBoxFor(m=>m[i].FolderWrite)
 </td>
 <td>
 @Html.TextBoxFor(m=>m[i].ExpireDate,new{type="date"}) // HTML5 date w/datepicker for browsers that support it
 </td>
 </tr>
}
<style>
 #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 };
*/
</style>
<script>
 $('#mytable').on("change","input",function(){
 var folderid=$(this).closest("tr").data("something");
 changePermissions(folderid);
 });
</script>

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

answered May 29, 2015 at 19:57
\$\endgroup\$
1
  • \$\begingroup\$ Yes, I already changed it to something like what you just typed, though your solution is much more refined. Thank you. \$\endgroup\$ Commented May 30, 2015 at 13:01

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.