skip to Main Content

I am currently working on parts of a new website for our company built in C# MVC4 using the Razor view engine. We are using Twitter Bootstrap 3 to provide a basic theme, and are using the Html helpers from TwitterBootstrapMVC to generate the Html markup.

The task I am currently working on is to create a Razor helper (lives in App_Code/) that will automatically generate and populate a table from a list of items.

To begin with, I am trying to construct the header row, however, whenever I try to introduce a loop to generate the cells for the header (body and footer does the same), I am getting the following error:

CS1502: The best overloaded method match for `System.Web.WebPages.HelperPage.WriteTo(System.IO.TextWriter, object)' has some invalid arguments

The code I am attempting to use is as follows:

@helper NewGridView(object data, string[] headers) {
    // data is to be a list/array of items
    var Html = ((System.Web.Mvc.WebViewPage) WebPageContext.Current.Page).Html;
    using (var table = Html.Bootstrap().Begin(new Table())) {
        using (var head = table.BeginHeader ()) {
            using (var row = head.BeginHeaderRow ()) {
                //for (int i = 0; i < headers.Length; i++)
                foreach (string header in headers) {
                    @row.Cell ("cell");    // had a static string here to test that the issue wasn't with the value from the array

If I remove the foreach loop (for loop doesn’t work either), and leave the @row.Cell(...) call, it works, but with either the for or foreach loop inplace, it generates the above error when I refresh the website.

Is there something I’m missing about Razor views and loops, or is what I attempting to do impossible?



  1. Chosen as BEST ANSWER

    Late answer, ended up resolving this after a few days but got sidetracked with other aspects of the project and wound up forgetting to post the answer. Better late than never.

    It turns out the compiler is really picky about usage of spaces in some contexts. The following generates the error described in the original post.

    foreach (string header in headers) {
        @row.Cell ("cell");

    but the following works fine.

    foreach (string header in headers) {

    Seems as if the compiler doesn't like the space in between the function name and the bracket when placed inside a foreach loop for some reason.

  2. As headers is the string array, So you can simply use this to get row value.

    @header instead of @row.Cell ("cell");

     foreach (string header in headers)
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top