简体   繁体   中英

How to Create Grid.Mvc From Controller?

I'm using Grid.Mvc in my project. I want to make optional Grid Properties by user. For example, one of user want to see grid title as "Schema", another user want to see as "Schema Name". I'll controll it from database.

So, I need to create below code from controller and write in cshtml. How can I do it?

@Html.Grid(Model).Columns(columns =>  
                    {  
                        columns.Add(c => c.SchemeID).Titled("Scheme ID").Filterable(true);  
                        columns.Add(c => c.SchemeName).Titled("SchemeName").Filterable(true);  
                        columns.Add()  
                        .Encoded(false)  
                        .Sanitized(false)  
                        .SetWidth(30)  
                        .RenderValueAs(o => Html.ActionLink("Edit", "Edit", new { id = o.SchemeID }));  

                    }).WithPaging(10).Sortable(true)  

You don't need to create the grid from the controller, what you need is to save the user settings and pass it to the view model, then use it in the view to create the grid.

ex: Lets say you created a class called UserGridSettings which has a property called SchemaColumnTitle , you can add whatever you want to it.

In your view model, you will add a property of type UserGridSettings and use it in the view as below:

@Html.Grid(Model).Columns(columns =>  
                    {  
                        columns.Add(c => c.SchemeID).Titled("Scheme ID").Filterable(true);  
                        columns.Add(c => c.SchemeName).Titled(Model.GridSettings.SchemaColumnTitle).Filterable(true);  
                        columns.Add()  
                        .Encoded(false)  
                        .Sanitized(false)  
                        .SetWidth(30)  
                        .RenderValueAs(o => Html.ActionLink("Edit", "Edit", new { id = o.SchemeID }));  

                    }).WithPaging(10).Sortable(true)  

Of course, you can make the model has a list of UserGridSettings and inside the @Html.Grid , you can iterate over this list and build the columns dynamically.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM