I created the worst soup soup of all time (ASP.NET MVC 2)

I look at the worst mess, my monitor is not tall enough to see what is happening, and VS 2010 does not help at all.

I do not know how to reorganize this garbage.

It's 11 a.m., and I feel like I'm already pouring a drink.

Is this just the ultimate proof that I don't have business coding? Be honest.

<div id="followedFriends">
    <% if (Model.FollowedFriends.Count() > 0)
       {
           foreach (var friend in Model.FollowedFriends)
           { %>
    <div id="friendContainer">
        <div class="followedFriend">
            <div class="userName">
                <%= Html.ActionLink(friend.FoFriend.UserName, "Visitor", "Home", new {userID = friend.FoFriend.UserId}, null)%></div>
            Currently reading:
            <br />
            <div class="bookLinks">
                <% if (friend.BookCount != 0)
                   { %>
                <% if (friend.BookCount <= 5)
                   { %>
                <%= friend.BookLinks%>
                <%}
                   else
                   { %>
                <%:Html.ActionLink(friend.BookCount + " different books.", "Visitor", "Home", new {userID = friend.FoFriend.UserId}, null)%>
                <%}
                   }
                   else
                   { %>
                Nothing, it appears...
                <%}%>
            </div>
            <%if (friend.ReviewCount != 0)
              {%>
            New review for:
            <div class="reviewLinks">
                <%if (friend.ReviewCount <= 5)
                  { %>
                <%= friend.ReviewLinks%>
                <%}
                  else
                  {%>
                <%: friend.ReviewCount %>
                different books
                <%}%></div>
            <%}

              if (friend.QuoteCount != 0)
              {%>
            <span class="highlight">&#9656</span>
            <%: friend.QuoteCount%>
            new
            <%if (friend.QuoteCount != 1)
              { %>quotes
            <%}
              else
              { %>
            quote
            <%} %>
            <%}%>
        </div>
    </div>
    <%}
       }%>
</div>
<%} %>

Update

As someone asked, here is the relevant part of the View Model:

 public class FollowedFriend
    {
        public aspnet_User FoFriend { get; set; }
        public string BookLinks { get; set; }
        public int BookCount { get; set; }
        public string ReviewLinks { get; set; }
        public int ReviewCount { get; set; }
        public int QuoteCount { get; set; }

        public FollowedFriend(Guid userID, DateTime lastVisit)
        {
            using (var context = new BookNotesEntities())
            {
                FoFriend = context.aspnet_Users.SingleOrDefault(u => u.UserId == userID);
                var reading = context.Books.Where(b => b.UserID == userID && b.CurrentlyReading == true).ToList();
                BookCount = reading.Count;
                if (BookCount <= 5)
                    BookLinks = Book.ConvertBooksToLinks("Book/Details", reading);
                else
                    BookLinks = "";
                var recentBooks = context.Books.Where(b => b.UserID == userID
                    && b.Review.DateCreated >= lastVisit).OrderByDescending(b => b.DateCreated).ToList();
                if (recentBooks.Count <= 5)
                    ReviewLinks = Book.ConvertBooksToLinks("/Book/Details", recentBooks);
                else
                    ReviewLinks = "";
                ReviewCount = recentBooks.Count;
                QuoteCount = context.Quotes.Count(q => q.UserID == userID && q.DateCreated >= lastVisit);
            }
        }
    }
+3
source share
7 answers

, : "if" - , , , , A.) HtmlHelper, B.) ViewModel.

, , ( ):

   <div id="followedFriends">
        <% foreach (var friend in Model.FollowedFriends) { %>
        <div id="friendContainer">
            <div class="followedFriend">
                <div class="userName">
                    <%: Html.ActionLink(friend.FoFriend.UserName, "Visitor", "Home", new {userID = friend.FoFriend.UserId}, null)%>
                </div>
                Currently reading:

                <br />
                <div class="bookLinks">
                    <%: Html.DisplayBooklinks(friend) %>
                </div>
                <div class="bookReviews">
                    <%: Html.DisplayBookReviews(friend) %>
                </div>
                <div class="bookQuotes">
                    <%: Html.DisplayQuotes(friend) %>
                </div>
            </div>
        </div>
        <% } %>
    </div>

, , - , . , :

<% Html.RenderPartial("FriendDetails", Model.FollowedFriends); %>

, , , , . , , : - . , , .

+8

HtmlHelperExtension, DisplayTemplate "Friend"

<div id="followedFriends">
    <% if (Model.FollowedFriends.Any()) {
        foreach (var friend in Model.FollowedFriends) {
            <%=Html.DisplayFor(friend) %>
       <% } %>
    <% } %>
</div>

DisplayTemplate "Friend" :

<div id="friendContainer">
    <div class="followedFriend">
        <div class="userName">
            <%= Html.ActionLink(friend.FoFriend.UserName, "Visitor", "Home", new {userID = friend.FoFriend.UserId}, null)%></div>
            Currently reading:
            <br />
            <%=Html.BookInformation(friend) %>
            <%=Html.BookReview(friend) %>
            <%=Html.Quotes(friend) %>
        </div>
    </div>
</div>

, HtmlHelperExtensions, DisplayTemplates , , : DisplayFor("BookReview", friend)

+2

, ...

"":

  • . , <div>, , , .
  • -. , . (ViewModel, ), -. , , "" "", ViewModel "DisplayValue".
  • HTML-. , if/else, , - , .

, , , : ! , , ----. , , , .

+1

ASP.Net MVC, :

  • % lt > ( <%: <% =)

  • % > <% ( <%: <% =)

  • # HTML

  • indent <% = <%: HTML ( HTML-/)

+1

ASP, , PHP, -? ? , HTML?

0

-, . , , , , , . , .

0

, . :

  • ELSE ", "
  • IF IF (count!= 0 && count < 5)
0

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


All Articles