Navigation: DbNetEditCore >

Constructors

 

 

 

 

 

DbNetEditCore is server-side ASP.NET Core form component designed to be used in a Razor view.

 

DbNetEditCore(string connection, string tableName, string id = null)

 

The constructor has 2 mandatory properties which should be passed to the constructor which are connection and tableName

 

...

@(new DbNetEditCore("northwind", "customers").Render())

...

 

Demo

 

It also has a 3rd optional parameter:

 

id  Specifies the Id of an existing element on the page that want the control to be rendered inside of 

 

Constructor when rendering in an exisiting HTML element

 

...

<div class="source">

    <div class="row">

        <div class="col">

            <h5>Customers</h5>

            <div id="customersEdit"></div>

        </div>

    </div>

    <div class="row">

        <div class="col">

            <h5>Orders</h5>

            <div id="ordersEdit"></div>

        </div>

    </div>

    <div class="row">

        <div class="col">

            <h5>Order Details</h5>

            <div id="orderDetailsEdit"></div>

        </div>

    </div>

</div>

...

 

Note the 3rd parameter specifies the Id of the HTML element inside of which the grid will be rendered

 

...

DbNetEditCore orderDetailsEdit = new DbNetEditCore("northwind", "[order details]", "orderDetailsEdit");

orderDetailsEdit.Column("OrderID").ForeignKey().Hidden();

orderDetailsEdit.Column("ProductID").Lookup(new Lookup("Products", "ProductId", "ProductName")).Label("Product");

orderDetailsEdit.Column("UnitPrice").Format("c");

orderDetailsEdit.LayoutColumns = 3;

 

DbNetEditCore ordersEdit = new DbNetEditCore("northwind", "orders", "ordersEdit");

ordersEdit.Column("CustomerID").ForeignKey().Hidden();

ordersEdit.Column("EmployeeID").Lookup(new Lookup("Employees", "EmployeeId", "lastname + ',' + firstname")).Label("Employee");

ordersEdit.Column("ShipVia").Lookup(new Lookup("Suppliers", "SupplierId", "CompanyName"));

ordersEdit.Column("Freight").Format("c");

ordersEdit.LayoutColumns = 3;

ordersEdit.AddLinkedControl(orderDetailsEdit);

 

DbNetEditCore customersEdit = new DbNetEditCore("northwind", "customers", "customersEdit");

 

customersEdit.Column("CustomerID").Hidden();

customersEdit.AddLinkedControl(ordersEdit);

customersEdit.LayoutColumns = 3;

 

@customersEdit.Render()

...

 

Demo

 

Specifying the database type in the constructor

 

If you are using a database other than MS SQL Server or SQLite which have their Data Providers included in the .Net Framework then you will need to install the required Data Provider for the database you are connecting to and to also specify the type of database in the constructor

 

...

DbNetEditCore customersEdit = new DbNetEditCore("sakila", DatabaseType.MySQL, "customers");

customersEdit.Column(new string[] { "company", "last_name", "first_name" }).Browse();

@customersEdit.Render()

...

 

 

Consuming a list of typed objects (List<T>) as a data source

 

If you want to use a List of objects perhaps created by an API you need to specify a DataSourceType of List in the constructor and then use the AddList method to assign the list of objects to form control. The changes can then be sent back to the API using the onJsonUpdated event

 

...

DbNetEditCore ProductsEdit = new DbNetEditCore(DataSourceType.List);

ProductsEdit.AddList(Model.Products);

ProductsEdit.Column(nameof(Product.ProductID)).PrimaryKey();

ProductsEdit.Column(nameof(Product.UnitPrice)).Format("c");

ProductsEdit.Column(nameof(Product.Discontinued)).DataType(typeof(bool));

ProductsEdit.Column(nameof(Product.SupplierID)).Lookup(Model.SupplierLookup);

ProductsEdit.Column(nameof(Product.CategoryID)).Lookup(Model.CategoryLookup);

ProductsEdit.Bind(EventType.onJsonUpdated, "applyJsonChanges");

@ProductsEdit.Render()

...

 

 

 

 

 

 

 

Copyright © 2023 DbNetLink