你總是可以執行下列色情:
<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;
}
}
謝謝,我選擇了原始色情。我猜只是一種習慣。 – Liquid
我已經提供了一個如何正確使用它的例子,但是如果你對使用它的色情片感到滿意。 –
感謝您的合適範例。事實上,外觀和感覺不那麼骯髒。 – Liquid