0

I am new to asp.net mvc 3.I am trying to generate a dynamic gridview using mvc3 but i cannot produce grid. My code below:

Model:


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

Controller


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

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

    }

View:


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

i want to create


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

dynamically return controller action. How do I return dynamic column or dynamic gridview or extentions?

tereško
  • 58,060
  • 25
  • 98
  • 150
loki
  • 2,926
  • 8
  • 62
  • 115
  • Can you be more clear, dynamic columns? What exactly you mean by dynamic columns? – Hari Gillala Jun 28 '12 at 09:46
  • Do you mean that you want to have each Column read a dynamic value? Or literally "FirstName" as column 1, "LastName" as column 2, and "Salary" as column 3? – Ecnalyr Jun 28 '12 at 11:24
  • possible duplicate of [ASP.NET MVC $.post call returning string...need help with format for jqGrid](http://stackoverflow.com/questions/4101116/asp-net-mvc-post-call-returning-string-need-help-with-format-for-jqgrid) – loki Jun 28 '12 at 12:45

2 Answers2

1

@programmerist, This is your Answer ;)

@{
    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)
}
Ramin Bateni
  • 16,499
  • 9
  • 69
  • 98
0

Try This. In this way you can create a Grid:

<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>
Tohid
  • 6,175
  • 7
  • 51
  • 80
RL89
  • 1,866
  • 5
  • 22
  • 39