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...

问题:

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 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.


回答1:

A small change in my controller and create a partial view and load that partial view in to 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 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 in to a div where I wanted to display the table

  • 发表于 2019-01-17 23:23
  • 阅读 ( 259 )
  • 分类:网络文章

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除