I am trying to format a WebGrid column so I can concatenate two pieces of data together (first and last name). Here is what I’m trying, but I will admit I don’t think I fully understand how to use the data (besides the basic scenarios) in WebGrid. (To the column name, the Teacher object is being passed to my view, but to actually get the teacher’s name, I have to get that information from the linked user object since users can play multiple roles.)
grid.Column(
columnName: "Teacher.User",
header: "Teacher",
style: "",
canSort: true,
format: (item) =>
{
var text = Html.DisplayTextFor(item => item.LastName) + ", " + Html.DisplayTextFor(item => item.FirstName);
return Html.Raw(text);
}
)
Strongly typed helpers that take lambda expressions don’t work with dynamic expressions which is what the
WebGridhelper is drowned with. Theitemargument that is passed to theformatfunction is of typedynamicso you cannot use it with lambda expressions.What an excellent candidate for a view model. Just add another property in your User view model:
that you will use in the view: