Extra if statements or re-code in C #

Trying to figure out what makes more sense

<%foreach (var item in Model.items)
   {
%>
<tr>
    <td>
        <% if (!item.isMgmt)
           {  %>
        <a href="/MVC/AzureMail/Unfiled/<%:item.uName %>">
            <%:item.uName%></a>
        <% }
           else
           { %>
        <%:item.uName %>
        <% } %>
    </td>
</tr>
<% } %>

or

 <%foreach (var item in Model.items)
   {
%>
<tr>
    <td>
        <% if (!item.isMgmt)
           {  %>
        <a href="/MVC/AzureMail/Unfiled/<%:item.uName %>">
        <% } %>
              <%:item.uName%>
        <% if (!item.isMgmt)
           {  %>
              </a>
        <% } %>
    </td>
</tr>
<% } %>
+3
source share
6 answers

third option; extension method for conditional link.

public static string ConditionalHyperlink(this HtmlHelper helper, string url, string text, bool shouldLink){
 ...
}

This greatly simplifies viewing.

<%= Html.ConditionalHyperlink("/MVC/AzureMail/Unfiled/" + item.Name, item.Name, item.isMgmt) %>
+12
source

First option. It seems more logical to have all the associated logic that creates the link in the expression, and not in the split in option 2.

Edit: I think most agree that option 1 is better. I am a supporter of HtmlHelpers (= cleaner views), so my additional suggestion was that you create an assistant that wraps the logic you represent.

+2
source

, , , . .

+1

, , href . , # 2.

,

+1

, , . , . , , , RenderName, HREF, isMgmt true , . , , :

<%foreach (var item in Model.items)
   {
%>
<tr>
    <td>
        <%:item.RenderName %>
    </td>
</tr>
<% } %>

, , .

0
<%foreach (var item in Model.items)
{
%>
<tr>
    <td>
        <%: item.isMgmt ? item.uName : string.format("<a href=\"/MVC/AzureMail/Unfiled/{0}\"">{0}</a>, item.uName) %>
    </td>
</tr>
<% } %>

html-

0

Source: https://habr.com/ru/post/1756567/


All Articles