English 中文(简体)
页: 1
原标题:asp.net MVC3 custom helper not working

我制定了以下法典,以便从以下阵列中得出一个表。

var fruit = new string[] { "apple", "pear", "tomato" };

   public static MvcHtmlString CustomGrid(this HtmlHelper htmlHelper, String Id, IList Items, IDictionary<string, string> Attributes)
        {
        if (Items == null || Items.Count == 0 || string.IsNullOrEmpty(Id))
            return MvcHtmlString.Empty;

        return BuildGrid(Items, Id, Attributes);

    }

    public static MvcHtmlString BuildGrid(IList Items, string Id, IDictionary<string, string> attributes)
    {
        StringBuilder sb = new StringBuilder();
        BuildHeader(sb, Items[0].GetType());

        foreach (var item in Items)
        {
            BuildTableRow(sb, item);
        }

        TagBuilder builder = new TagBuilder("table");
        builder.MergeAttributes(attributes);
        builder.MergeAttribute("name", Id);
        builder.InnerHtml = sb.ToString();
        var Tag = builder.ToString(TagRenderMode.Normal);
        return MvcHtmlString.Create(Tag);
    }

    public static void BuildTableRow(StringBuilder sb, object obj)
    {
        Type objType = obj.GetType();
        sb.AppendLine("	<tr>");
        foreach (var property in objType.GetProperties())
        {
            sb.AppendFormat("		<td>{0}</td>
", property.GetValue(obj, null));
           // sb.AppendFormat("		<td>{0}</td>
", obj);
        }
        sb.AppendLine("	</tr>");
    }

    public static void BuildHeader(StringBuilder row, Type p)
    {
        row.AppendLine("	<tr>");

        foreach (var property in p.GetProperties())
        {
            row.AppendFormat("		<th>{0}</th>
", p.Name);
        }
        row.AppendLine("	</tr>");
    }

但它没有做任何事情。 我也这样做:

Html.CustomGrid("myTable", (System.Collections.IList)fruit, null);

请提供解决办法。

最佳回答

首先在一些简编声明中,以确保任何thing来自产出。 否则,你很可能不使用@Html。 海关,而不是在没有对对应措施进行产出的编码中加以使用。

问题回答

You re missing the @ from Html.CustomGrid("myTable", (System.Collections.IList)fruit, null);





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签