Created
September 24, 2011 20:31
-
-
Save moniyax/1239823 to your computer and use it in GitHub Desktop.
Admin Links In View
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//source: http://stackoverflow.com/questions/1236739/how-can-i-avoid-unmaintanable-code-in-asp-net-mvc-views | |
<td> | |
Html.LinkList(", " | |
ActionLinks.ViewDetails(item), | |
ActionLinks.DeleteAndConfirm(item), | |
ActionLinks.Approve(item)) | |
</td> | |
class ActionLinks | |
{ | |
public static string Approve(Item item) | |
{ | |
if(ItemRequiresApproval(item) && CurrentUserIsAdmin()) | |
{ | |
return Html.ActionLink("Approve", "Approve", new { id = item.Mail_ID }); | |
} | |
else | |
{ | |
return string.Empty; | |
} | |
} | |
private static bool ItemRequiresApproval(Item item) | |
{ | |
//determine whether item requires approval | |
//this could be further broken into a separate utilities class | |
} | |
private static bool CurrentUserIsAdmin() | |
{ | |
//this should definitely go in a separate class dedicated to | |
//handling membership and authorization | |
//as well as figuring out who the current user is | |
} | |
} | |
string LinkList(string delimiter, params string[] links) | |
{ | |
StringBuilder sb = new StringBuilder(); | |
foreach(string link in links) | |
{ | |
if(!string.IsNullOrEmpty(link)) | |
{ | |
sb.Append(delimiter); | |
sb.Append(link); | |
} | |
} | |
return sb.ToString().Substring(delimiter.Length); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment