I'm using MVC's WebGrid throughout my application and it's been working great. I'm setting the columns for the grid in my controller code and then passing that to my view. I add a new column like this where columns
is a List<MvcWebGridColumn>
:
columns.Add(new WebGridColumn { ColumnName = "Path", Header = "Full Path", Filter = ... });
And then in my view, I set the grid columns from my view model like this:
@grid.GetHtml(
footerStyle: "hide",
mode: WebGridPagerModes.All,
htmlAttributes: new { id = @Model.Id },
columns: @Model.Columns)
Up until this point, setting the columns in the code has worked great as the columns have been known, first-level properties of my models. Now things are more complicated and I don't know how to handle it with the WebGrid. Here is an example model:
public class Requirement
{
public string Id { get; set; }
public string Path { get; set; }
public Dictionary<string, string> Fields { get; set; }
}
I want to be able to set a WebGrid's column in the code using the value from the Field
dictionary property's key. I'm trying this, but it's not working:
columns.Add(new WebGridColumn { ColumnName = String.Format("Fields[\"{0}\"]", key), Header = label });
I tried also putting a method GetFieldValue(string key)
method on my Requirement
model, but it doesn't like that either. It seems like it can only handle exactly matching an object's property identifier.
I'm not sure if this is possible using WebGrid out of the box or if I have to extend the functionality. Thanks in advance.