[英]Multiple html classes with c#
我想在mvc3 c#中向表中添加更多类。
我现在有这个:
<tr class=@(item.cancelled ? "cancelled" : "")>
是否可以在这里添加额外的类,例如:
<tr class= "cancelled added">
<tr class="@(item.cancelled ? "cancelled" : "") added">
使用它的更好方法是这样的:
@using (Html.Td(item, isBlocked))
{
<div>some contents for the td</div>
}
like this:
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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.