如何在webgrid + MVC 3中使用foreach

时间:2013-05-24 05:41:12

标签: asp.net-mvc-3 webgrid

我在MVC中有如下的webgrid:如何在webgrid中的列中的单行中显示值列表。我在webgrid列中使用foreach但是它无法识别

  Col1  Col2  Col3
    ---------------
    1    | zzz | t
         | xxx |
         | yyy |
    ---------------
    2    | aaa | P
         | bbb |
         | ccc |

以上网格只是示例示例..我想显示数据。 在第二列中,我想显示单行中的值列表。我试过的代码,但是我得到错误foreach在webgrid中无效。

在模特:

 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; }
    }

在控制器中:

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);
        }

在视图中:

 @{       
                          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>"
                                                     ); })

                                    )
                            )

0 个答案:

没有答案