How to Pass Model Dynamically ?

Jun 14, 2011 at 2:48 PM

hi

how to pass the model data dynamically(underlined)


@Html.Grid(new GridControl()
        .SetName("grid")
        .SetPageSize(10)
        .SetIsAutoSize(true)      
        .SetListUrl(ViewData["ListURL"].ToString())
        .SetEditUrl("/Renderers/EditBank")
        .SetHeight("'100%'")
        .SetHeight("200")      
        .UseColumns(
Columns.BankData)
            .UpdateDefaultPager(pager =>
                pager
                    .ShowAdd(true)
                    .ShowDel(true, deleteUrl: "/Renderers/DeletePerson")
                    .ShowEdit(true)
                    .ShowView(true)
                ))

Columns. is having many classes like BankData, DeptData e.t.c
At run time i have to set this is it possible.... If Possible guide me with code...
Coordinator
Jun 14, 2011 at 3:07 PM

I'm not quite sure I understand your question.

If you ask how to pass data to the grid without having making additional request to the controller, you can use the SetDataSource method the GridControl,

If you want to create a grid but have no one model that is sufficient, you can use the following API (instead of the strongly-typed UseColumns API):

@Html.Grid(new GridControl()...
.AddColumn("Name")
.AddColumn("CityName")
...

And in the controller's action you can use LinQ to generate a new anonymous type or Dynamics etc.

Did I understand you correctly?

Jun 15, 2011 at 5:30 AM

My actual requirement is, i will use single grid controls to display different tables data according to requirement. At the same time for insert, update, delete also i am following your methods.

I hope u can find in my first post

.SetListUrl(ViewData["ListURL"].ToString())

this URL i am sending dynamically.

Like that i am able to send for the rest  like....

.SetEditUrl("/Renderers/EditBank")
deleteUrl:

But the main thing is

.UseColumns(Columns.BankData)

I am not sure what will be the column names bcoz i am using same grid for different tables.

So, my question is, whether it's possible to Send the Columns.BankData dynmically (Columns may contain BankData, DeptData, SalData e.t.c)

Some times i have send Columns.BankData or Columns,DeptData .......




Developer
Jun 15, 2011 at 6:51 AM
That just seems backwards, why would you want a single grid to show different data at different times? You can easily write code to determine which out of several grids you wish to show at render time; that seems like a much cleaner solution.
with regards,
mattias

Från: vijayreddy59 [notifications@codeplex.com]
Skickat: den 15 juni 2011 06:30
Till: Mattias Fagerlund
Ämne: Re: How to Pass Model Dynamically ? [MVCjQueryControls:261324]

From: vijayreddy59

My actual requirement is, i will use single grid controls to display different tables data according to requirement. At the same time for insert, update, delete also i am following your methods.

I hope u can find in my first post

.SetListUrl(ViewData["ListURL"].ToString())

this URL i am sending dynamically.

Like that i am able to send for the rest like....

.SetEditUrl("/Renderers/EditBank")
deleteUrl:

But the main thing is

.UseColumns(Columns.BankData)

I am not sure what will be the column names bcoz i am using same grid for different tables.

So, my question is, whether it's possible to Send the Columns.BankData dynmically (Columns may contain BankData, DeptData, SalData e.t.c)

Some times i have send Columns.BankData or Columns,DeptData .......




Coordinator
Jun 15, 2011 at 7:01 AM

Well the Html.Grid extension method provided by the MVC.Controls library renders a grid using a GridControl object,
so you could solve your problem by either dynamically building the entire GridControl in the Controller and passing it to the view as part of the ViewData dictionary,
or very much like the example you posted, you could dynamically build a GridColumnModelList<T> in your controller (where each controller action will create a different GridColumnModel with different\same T) and pass it to the view using the ViewData dictionary,
or you could do something like the following inside the actual view:

<%


GridControl grid = new GridControl().SetName("grid")
    .SetPageSize(10)
    .SetIsAutoSize(true);
    .SetListUrl(ViewData["ListUrl"].ToString())
    .SetEditUrl(ViewData["EditUrl"].ToString());

/* Create a column using criteria */
if (ViewData["someKey"] == some_object)
    grid.AddColumn("someColumn");

/* Create a column name from the ViewData */
grid.AddColumn(ViewData["ColumnName"].ToString())

/* Use a GridColumnModel from the ViewData to add a column */
grid.AddColumn(ViewData["ColumnModel"] as GridColumnModel);

%>

... Some HTML ...

<%= Html.Grid(grid) %>

 

Jun 15, 2011 at 10:03 AM

I am happy with ur reply

But i am still worried about how to pass entire grid from controller to Razor view.

I am able to do like this

            GridControl gg = new GridControl();
            gg.SetName("grid");
            gg.SetPageSize(10);
            gg.SetIsAutoSize(true);
            //.SetOnGridCompleteEvent("createActionControls")       
            gg.SetListUrl(ViewData["ListURL"].ToString());
            gg.SetEditUrl("/Renderers/EditBank");
            gg.SetHeight("'100%'");
            gg.UseColumns(Columns.BankData);

 

How to pass the gg to razor view and what i have to take care in view...

Coordinator
Jun 15, 2011 at 10:28 AM
Edited Jun 15, 2011 at 10:31 AM

You could either pass it as the return value of the action (and set the razor view to a strongly-typed GridControl view), or add it to the ViewData dictionary, and consume it from the razor view:

 

@Html.Grid((GridControl)ViewData["Grid"])

(P.S, using the Setter methods as in your example is possible though a bit awkward - either concatenate their calls, or sequentially set the conventional properties directly)