FINEUIMVC Essays-Dynamically CREATE TABLE columns

Source: Internet
Author: User

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:

  1. The MVC routing engine finds the appropriate controller (HomeController.cs) based on the URL.
  2. The operation method of the controller is about preparing the data and then passing in the view home/about.cshtml.
  3. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.