0

我是 asp.net mvc 3 的新手。我正在尝试使用 mvc3 生成动态网格视图,但我无法生成网格。我的代码如下:

模型:


  public class Employee
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public double Salary { get; set; }
        public static List<Employee> GetList()
        {
            List<Employee> employees = new List<Employee>{
          new Employee   { FirstName="Rahul", LastName="Kumar", Salary=45000},
          new Employee   { FirstName="Jose", LastName="Mathews", Salary=25000},
          new Employee   { FirstName="Ajith", LastName="Kumar", Salary=25000},
          new Employee   { FirstName="Scott", LastName="Allen", Salary=35000},
            new Employee   { FirstName="Abhishek", LastName="Nair", Salary=125000}
            };
            return employees;
        }
    }

控制器


  public class EmployeeController : Controller
    {
        //
        // GET: /Employee/

        public ActionResult Index()
        {
            var empoyees = Employee.GetList();
            return View(empoyees);
        }

    }

看法:


<%
    var grid = new WebGrid(source: Model, defaultSort: "FirstName", rowsPerPage: 3);
    using (Html.BeginForm())
    {
        %>
        <div id="grid">
        <%:grid.GetHtml(tableStyle:"grid", 
        headerStyle:"head", 
        alternatingRowStyle:"alt",
        columns:grid.Columns(
        grid.Column("FirstName"),
        grid.Column("LastName"),
        grid.Column("Salary"))) %>
        </div>
        <%} %>

我想创建


grid.Column("FirstName"),
        grid.Column("LastName"),
        grid.Column("Salary"))

动态返回控制器动作。如何返回动态列或动态网格视图或扩展?

4

2 回答 2

1

@programmerist,这是你的答案;)

@{
    var properties = typeof(MyModelClassName).GetProperties();
    var webGridColumns = properties.Select(prop => new WebGridColumn()
        {
            ColumnName = prop.Name, Header = prop.Name, Style = "my-style"
        }).ToList();

    var grid = new WebGrid(source: Model, rowsPerPage: 3);
    @grid.GetHtml(tableStyle: "grid",
                  headerStyle: "head",
                  alternatingRowStyle: "alt",
                  columns: webGridColumns)
}
于 2013-01-24T12:41:48.227 回答
0

尝试这个。通过这种方式,您可以创建一个网格:

<table>
 <tr>
     <th>
         Item1
     </th>
     <th>
         Item2
     </th>
     <th>
         Item2
     </th>
 </tr>

<% foreach (var item in Model) { %>
    <tr>
        <td>
            <%: item.item1 %>
        </td>
        <td>
            <%: item.item2 %>
        </td>
        <td>
            <%: item.item3 %>
        </td>           
    </tr>
<% } %>
</table>
于 2012-06-28T09:39:13.887 回答