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 に答える