2009-07-24 52 views
0

輸入:如何簡化本準則未完善

Id, PartId, Name 
1, 1, Head 
1, 2, body 
1, 3, Tail 
2, 1, Head 
2, 2, Leg 

輸出顯示:

- Head, Body, Tail [Delete(1)] 
- Head, Leg [Delete(2)] 

我的代碼:

<ol> 
<% 
    int prev = -1; 
    foreach (var item in t) 
    { 
     if(prev != item.ResponseId){ 
      if (prev != -1) 
      {%> 
       <%= Html.ActionLink("[replacethis]", "RemoveResponse", new { id = item.ResponseId }) 
       .Replace("[replacethis]", "<img src=\"../../Content/images/delete_icon.gif\" class=\"borderlessImage\" title=\"Remove\"/>")%>  
       </li> 
      <%} %> 
      <li> 
      <% } 
     else { 
      %>, <% 
     } %> 

     <%= Html.Encode(item.ResponsePartValue) %> 

    <% prev = item.ResponseId; 
    } %> 

    <%= Html.ActionLink("[replacethis]", "RemoveResponse", new { id = prev }) 
       .Replace("[replacethis]", "<img src=\"../../Content/images/delete_icon.gif\" class=\"borderlessImage\" title=\"Remove\"/>")%>  
       </li> 
</ol> 

問題:

  1. 什麼如何重構這個?
  2. 我錯過了任何MVC技巧?
+0

「我失蹤的任何MVC技巧?」 - 錯誤...就像使用它? – Draemon 2009-07-24 17:38:01

+0

@Draemon是非常有幫助的不是。 – kenny 2009-07-24 17:49:34

回答

1

那麼,首先,您可以創建一個HtmlHelper,爲您呈現圖片鏈接,而不是先生成定位標記,然後用圖片替換其內容。

看看here

此外,您每次需要輸出一些文本時都不必使用<%=。如果你已經有開放的代碼塊(即<%),那麼你可以使用Response.Write方法來輸出你想要的。在像你這樣的情況下,最有可能看起來比%> <%=好。

雖然,我承認我並不完全知道您在此處列出的內容以及您希望如何顯示。但是你的算法下,我想這是我會做:

<ol> 
<% 
    int prev = -1; 
    foreach (var item in t) { 
     if(prev != item.ResponseId) { 
      if (prev != -1) { 
       Response.Write(Html.ImageLink("../../Content/images/delete_icon.gif", "RemoveResponse", new { id = item.ResponseId, @class ="borderlessImage", title = "Remove" }) + "</li>"); 
      } 
      Response.Write("<li>"); 
     } 
     else { 
      Response.Write(", "); 
     } 
     prev = item.ResponseId; 
     Response.Write(Html.Encode(item.ResponsePartValue)); 
    } %> 

    <%= Html.ImageLink("../../Content/images/delete_icon.gif", "RemoveResponse", new { id = prev, @class ="borderlessImage", title = "Remove" }) %> 
    </li> 
</ol> 
0

我把一切都變成一本字典,它會使你的邏輯更符合邏輯:-)

喜歡的東西:

IDictionary<int, List<Part>> partsDictionary = new Dictionary<int, List<Part>>(); 

其中,int鍵是您的id,然後List類型的值將是您的各個部分。

然後把邏輯放到HtmlHelper擴展中。

E.g. (雖然我不知道你在做什麼,視圖代碼不匹配,你在上面DB模式。這應該給你一個想法)

public static string PartsList(this HtmlHelper html, IDictionary<int, List<Part>> partsDictionary) 
{ 
    if (partsDictionary.Count == 0) 
     return ""; 

    StringBuilder toReturn = new StringBuilder("<ol>"); 
    foreach (KeyValuePair<int, List<Part>> kvp in Model.PartsDictionary) 
    { 
     toReturn.Append("<li>"); 

     //Individual part links 
     IList<string> partsLinks = new List<string>(); 
     foreach (Part part in kvp.Value) 
      partsLinks.Add(html.ActionLink(part.PartName, "ActionName", new { @id = part.Id })); 

     toReturn.Append(string.Join(", ", partsLinks.ToArray())); 

     //Part category(?) link 
     toReturn.Append(html.ActionLink("Delete", "ActionName", new { @id = kvp.Key })); 

     toReturn.Append("</li>"); 
    } 

    toReturn.Append("</ol>"); 
    return toReturn.ToString(); 
} 

或者類似的東西;-)

HTHs
Charles