2012-06-20 56 views
0

我只想在某些情況下顯示我的標題屬性。我不希望它顯示條件未滿足時。現在它顯示一個空的工具提示。條件失敗時,我不需要提示工具提示。如何在剃刀下不顯示標題屬性?

清理有點

<tr class=title="@(item.Cancelled ? "Cancelled" : item.Confirmed ? isBlocked? "blocked date": **no title attribute here** :"Confirm needed") "> 

回答

3

你總是可以執行下列色情:

<tr @Html.Raw(item.Cancelled ? "title=\"Cancelled\"" : item.Confirmed ? isBlocked ? "title=\"blocked date\"": "" : "title=\"Confirm needed\"")> 

但我會建議你寫一個自定義的輔助生成這個td元素:

@using (Html.Td(item, isBlocked)) 
{ 
    <div>some contents for the td</div> 
} 

是這樣的:

public static class HtmlExtensions 
{ 
    private class TdElement : IDisposable 
    { 
     private readonly ViewContext _viewContext; 
     private bool _disposed; 

     public TdElement(ViewContext viewContext) 
     { 
      if (viewContext == null) 
      { 
       throw new ArgumentNullException("viewContext"); 
      } 
      _viewContext = viewContext; 
     } 

     public void Dispose() 
     { 
      this.Dispose(true); 
      GC.SuppressFinalize(this); 
     } 

     protected virtual void Dispose(bool disposing) 
     { 
      if (!this._disposed) 
      { 
       _disposed = true; 
       _viewContext.Writer.Write("</td>"); 
      } 
     } 
    } 

    public static IDisposable Td(this HtmlHelper html, ItemViewModel item, bool isBlocked) 
    { 
     var td = new TagBuilder("td"); 
     var title = item.Cancelled 
      ? "Cancelled" 
      : item.Confirmed 
       ? isBlocked 
        ? "blocked date" 
        : "" 
       : "Confirm needed"; 

     if (!string.IsNullOrEmpty(title)) 
     { 
      td.Attributes["title"] = title; 
     } 
     html.ViewContext.Writer.Write(td.ToString(TagRenderMode.StartTag)); 
     var element = new TdElement(html.ViewContext); 
     return element; 
    } 
} 
+2

謝謝,我選擇了原始色情。我猜只是一種習慣。 – Liquid

+1

我已經提供了一個如何正確使用它的例子,但是如果你對使用它的色情片感到滿意。 –

+0

感謝您的合適範例。事實上,外觀和感覺不那麼骯髒。 – Liquid