简体   繁体   中英

How to bind C# DataTable to JQUERY DataTable?

I want to bind DataTable get from Excel Sheet to a Jquery Datatable. And my aim is to bind all the columns for the Datatable as dynamically. I have no idea how to bind the columns dynamically.

C# and Jquery is the code base

In this code, I get the data from the excel sheet as a Datatable

region DataValidation

    public DataTable DataValidation(string dataExchangeSelectedColum, string entityvalue,string filename)
    {
        UA patsUA = Session["PaTSUA"] as UA;
        //List<DataExchangeDefinitionViewModel> dataExchangeDefinitionListVM = _mapper.MapToDataExchangeDefinitionViewModelList(_dataExchangeBusiness.ValidateDataType(dataExchangeSelectedColum, entityvalue, filename, patsUA.DBConnectionString));
        DataTable dataTable = _dataExchangeBusiness.DataValidation(dataExchangeSelectedColum, entityvalue, filename, patsUA.DBConnectionString);
        return dataTable;
    }
    #endregion DataValidation

I want to bind the above DataTable into jquery Datatable. The above Datatable may vary that is the columns vary in different situations. so columns must bind dynamically.

A small change in my controller and create a partial view and load that partial view into a div

Here is the code

Controller

#region DataValidation

    public ActionResult DataValidation(string dataExchangeSelectedColum, string entityvalue,string filename)
    {
        UA patsUA = Session["PaTSUA"] as UA;
        DataTable dataTable = null;
         dataTable = _dataExchangeBusiness.DataValidation(dataExchangeSelectedColum, entityvalue, filename, patsUA.DBConnectionString);


        return PartialView("_ExcelDataTable", dataTable);
    }
    #endregion DataValidation

Created a partial view. Here it comes to play

@model System.Data.DataTable
@using System.Data;
@{
    IEnumerable<DataRow> _excelDataRowList = from dataRow in Model.AsEnumerable() select dataRow;
}
<div class="table-responsive tableScroll">
    <table id="data-table-basic" class="table table-striped">
        <thead>
            @foreach (DataColumn col in Model.Columns)
            {
                <tr>
                    @col.Caption.ToString()
                </tr>
            }
        </thead>
        <tbody>
            @foreach (DataColumn dtCol in Model.Columns)
            {
                <tr>
                    @foreach (DataRow row in _excelDataRowList)
                    {
                        <td>
                            @row[dtCol]
                        </td>
                    }

                </tr>
            }
        </tbody>
    </table>
</div>

I load this partial view into a div where I wanted to display the table

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