3

我想在 mvc3 c# 中的表中添加更多类。

我现在有这个:

<tr class=@(item.cancelled ? "cancelled" : "")> 

是否可以在这里添加一个额外的类,例如:

<tr class= "cancelled added">
4

2 回答 2

4
 <tr class="@(item.cancelled ? "cancelled" : "") added"> 
于 2012-05-15T07:51:30.040 回答
1

更好的使用方法是这样的:

@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;
    }
}
于 2012-06-25T06:16:12.460 回答