Disclaimer: FINEUIMVC (Basic edition) is free software and this series of articles applies to the basic version.
User Requirements
Users want to implement dynamic creation of table columns, and in WebForms we do this by creating columns in Page_Init:
But in MVC, if you're thinking about the WebForms, it doesn't work to think about how you can access the table controls in the View in the Controller.
I have written a series of articles, "NET MVC QuickStart (MVC5+EF6)", which begins with the process of creating pages in MVC:
This page was presented to us and experienced three main processes:
- The MVC routing engine finds the appropriate controller (HomeController.cs) based on the URL.
- The operation method of the controller is about preparing the data and then passing in the view home/about.cshtml.
- The view prepares the HTML fragment, puts it in the layout page and returns to the browser.
The controller prepares the data, passes in the view, and then the view starts rendering the page. This means that when the controller executes, the contents of the view are unknown. It is important to understand this point.
Dynamically CREATE TABLE columns
Of course, it's not difficult to dynamically create table columns in MVC, and we need to pass the table column data (gridcolumn[]) to the view like a tabular data source.
Controller:
//Get:grid/dynamiccolumns PublicActionResult Index () {initgridcolumns (); returnView (Datasourceutil.getdatatable ());}Private voidInitgridcolumns () {List<GridColumn> columns =NewList<gridcolumn>(); Renderfield Field=NULL; Columns. ADD (NewRownumberfield ()); Field=NewRenderfield (); Field. HeaderText="Sex"; Field. DataField="Gender"; Field. FieldType=Fieldtype.int; Field. Rendererfunction="Rendergender"; Field. Width= the; Columns. ADD (field); Field=NewRenderfield (); Field. HeaderText="Year of entry"; Field. DataField="Entranceyear"; Field. FieldType=Fieldtype.int; Field. Width= -; Columns. ADD (field); Rendercheckfield CheckField=NewRendercheckfield (); CheckField. HeaderText="whether the school"; CheckField. DataField="Atschool"; CheckField. Renderasstaticfield=true; CheckField. Width= -; Columns. ADD (CheckField); // ...Viewbag.grid1columns=columns. ToArray ();}
In the controller, the List<gridcolumn> object is created dynamically and saved to Viewbag.grid1columns.
In a real-world project, this step might involve interacting with the database to get the table columns that need to be created dynamically.
View:
@{Viewbag.title="Grid/grid"; varF =@Html. F ();} @model system.data.datatable@section Body {@ (F.grid (). Enablecollapse (true) . Width (850) . Title ("tables (dynamically created columns)") . Showheader (true) . Showborder (true). ID ("Grid1") . Dataidfield ("Id") . DataTextField ("Name") . Columns (Viewbag.grid1columns). DataSource (Model))<br> <br>@ (F.button (). Text ("which rows are selected"). ID ("Button1") . Listener ("Click","notifyselectedrows (' Grid1 ');") )}
Page effect:
Summary
This article describes the methods for dynamically creating table columns in WebForms and MVC. By contrast, we also hope that we can fully understand the important differences between the two frameworks.
If you're just starting out with MVC, it's recommended to start with the three-stone series of tutorials: http://www.cnblogs.com/sanshi/p/6210695.html
FINEUIMVC Essays-Dynamically CREATE TABLE columns