1

i asked similar question before : How to create dynamic Grid using mvc 3 aspx engine? (Please don' ignore previous question)

if i write beelow code result first picture:


<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumarable<MvcAppGridView.Models.Employee>>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Index
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

<%
    var grid = new WebGrid(source: Model, defaultSort: "FirstName", rowsPerPage: 3);
 using (Html.BeginForm())
    {
        %>
        <div>
     <%:  grid.GetHtml(columns: grid.Columns(
                grid.Column(
                   header: "",
                   style: "text-align-center",
                   format: (item) => new HtmlString(Html.ActionLink("Edit", "Edit", new { id = item.id }).ToString() + " | " +
                                                    Html.ActionLink("Details", "Details", new { id = item.id }).ToString() + " | " +
                                                    Html.ActionLink("Delete", "Delete", new { id = item.id }).ToString()
                                                   )
                )
        ))
             %>
        </div>
        <%} %>
</asp:Content>

enter image description here

But i want to that dynamically like :


     <%:grid.GetHtml(tableStyle:"grid", 
        headerStyle:"head", 
        alternatingRowStyle:"alt",
        columns:grid.Columns(
        grid.Column("FirstName"),
        grid.Column("LastName"),
        grid.Column("Salary"),
          grid.Column(
                   header: "",
                   style: "text-align-center",
                   format: (item) => new HtmlString(Html.ActionLink("Edit", "Edit", new { id = item.id }).ToString() + " | " +
                                                    Html.ActionLink("Details", "Details", new { id = item.id }).ToString() + " | " +
                                                    Html.ActionLink("Delete", "Delete", new { id = item.id }).ToString()
                                                   )

        ))) %>

My desire result like that:

enter image description here

But i dislike below binding :

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

not like it! i want to second result automatically or dynamically but how?

Community
  • 1
  • 1
loki
  • 2,926
  • 8
  • 62
  • 115
  • Can't u use a Modal to add the column names? grid.Column(Modal.Firstname), grid.Column(Modal.Lastname), grid.Column(Modal.Salary) – Jeroen Doppenberg Oct 30 '13 at 14:25
  • You can create some structure in your model to define the table columns from the controller and then simply interate through the settings in your view and add the columns dynamically – Vojtech B May 06 '14 at 12:47

0 Answers0