user1654791
user1654791

Reputation: 31

how to format html helpers in webgrid column

var grid = new WebGrid(Model, rowsPerPage: 10, canPage: true);

 @grid.GetHtml(
       columns: new[]
       {
       grid.Column("CompanyName",
                    style: "col1",
                    format:@Html.TextBoxFor(modelItem => modelItem.CompanyName,new{@class="edit-mode"})
    )
})

at format its showing like this

cannot convert from html string to function <dynamic object>

I have also tried

format: item => Html.TextBox(
        (string)item.CompanyName
    )

how do i format html helpers in webgrid ?

Upvotes: 0

Views: 4113

Answers (2)

Darin Dimitrov
Darin Dimitrov

Reputation: 1039498

Try like this:

grid.Column(
    "CompanyName",
    style: "col1",
    format: item => Html.TextBoxFor(
        modelItem => modelItem.CompanyName, 
        new { @class="edit-mode" }
    )
)

But I guess that's not what you want, because I guess that your view is bound to a different model.

What you need instead is the following:

grid.Column(
    "CompanyName",
    style: "col1",
    format: 
        @<text>
            @{ var index = Guid.NewGuid().ToString(); }
            @Html.Hidden("HeaderItems.Index", index)
            Html.TextBox(
                "[" + index + "].CompanyName", 
                item.CompanyName, 
                new { @class="edit-mode" }
            )
        </text>
)  

and now the view you will be posting to could be strongly typed to a collection of your view model:

[HttpPost]
public ActionResult SomeAction(IEnumerable<CompanyViewModel> model)
{
    ...
}

Upvotes: 2

Huske
Huske

Reputation: 9296

One way of doing it would be like:

Format: m => Html.TextBoxFor(mdelItem => modelItem.CompanyName, new {@class="edit-mode"})

Upvotes: 0

Related Questions