I have webgrid as below in MVC:how to show list of values in a single row in column in webgrid.I am using foreach inside webgrid column but it is unrecognized
Col1 Col2 Col3
---------------
1 | zzz | t
| xxx |
| yyy |
---------------
2 | aaa | P
| bbb |
| ccc |
Above grid is just sample Example..i want to show data. In 2nd column i want to show list of values in single row.Below code I tried,But i get Error foreach is invalid in webgrid.
In Model:
public class Employee
{
public string EmployeeId { get; set; }
public string EmployeeName { get; set; }
public string RowId { get; set; }
public string Error { get; set; }
public List<string> tbldata { get; set; }
public List<Employee> lstEmp { get; set; }
}
In Controller:
public ActionResult Index()
{
Employee emp = new Employee();
List<string> tbldata3 = new List<string>();
tbldata3.Add("xxx");
tbldata3.Add("yyy");
tbldata3.Add("zzz");
List<string> tbldata1 = new List<string>();
tbldata1.Add("gggh");
tbldata1.Add("hhhh");
tbldata1.Add("ffff");
List<string> tbldata2 = new List<string>();
tbldata2.Add("ppp");
tbldata2.Add("oooo");
tbldata2.Add("iii");
List<Employee> lstempo = new List<Employee>();
lstempo.Add(new Employee { EmployeeId = "100", EmployeeName = "aaa", RowId = "111", Error = "3434", tbldata = tbldata1 });
lstempo.Add(new Employee { EmployeeId = "101", EmployeeName = "BB", RowId = "222", Error = "6767", tbldata = tbldata2 });
lstempo.Add(new Employee { EmployeeId = "102", EmployeeName = "ccc", RowId = "333", Error = "898", tbldata = tbldata3 });
emp.lstEmp = lstempo;
return View(emp);
}
In View:
@{
var grid = new WebGrid(source: Model.lstEmp,
canSort: true,
rowsPerPage: 10,
ajaxUpdateContainerId: "grdCurrentReqDetails"
);
}
@grid.GetHtml(
tableStyle: "webGrid",
headerStyle: "gridHead",
alternatingRowStyle: "alt",
columns: grid.Columns
(
grid.Column("EmployeeId", header: "EmployeeId"),
grid.Column("EmployeeName", header: "EmployeeName"),
grid.Column("RowId", header: "RowId"),
grid.Column("Error", header: "Error"),
grid.Column("Checks", format: (item) => { return new HtmlString(
"<table><tr><td>"+
foreach(var t in item.tbldata)
{
t.ToString();
}
+"</td></tr></table>"
); })
)
)