首页 > 解决方案 > 在 c# 和 mvc 中的类之间传递数据表

问题描述

我一直在用 c# 编程并且有一个关于在类中创建数据表然后尝试从另一个类或 mvc 控制器访问该数据表的问题。一些建议和一些解释将不胜感激

当前班级

using OfficeOpenXml;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using ppms_excel.Models;



namespace ppms_excel.Models
{

    public class PODataTable
    {

        public DataTable LDataTable
        {
            get
            {
                DataTable legalEntityTable = new DataTable("legal_entitiy");
                legalEntityTable.Columns.Add("Id", typeof(int));
                legalEntityTable.Columns.Add("ExternalId", typeof(string));
                legalEntityTable.Columns.Add("Source", typeof(string));
                legalEntityTable.Columns.Add("XpressEntityId", typeof(int));
                legalEntityTable.Columns.Add("EntityTypeId", typeof(int));
                legalEntityTable.Columns.Add("EntityName", typeof(string));
                legalEntityTable.Columns.Add("EIN", typeof(string));
                legalEntityTable.Columns.Add("Descripton", typeof(string));
                legalEntityTable.Columns.Add("Address1", typeof(string));
                legalEntityTable.Columns.Add("Address2", typeof(string));
                legalEntityTable.Columns.Add("City", typeof(string));
                legalEntityTable.Columns.Add("State", typeof(string));
                legalEntityTable.Columns.Add("PostalCode", typeof(string));
                legalEntityTable.Columns.Add("WebSiteUrl", typeof(string));
                legalEntityTable.Columns.Add("PricingAgreementTime", typeof(DateTimeOffset));
                legalEntityTable.Columns.Add("AgreementTime", typeof(DateTimeOffset));
                legalEntityTable.Columns.Add("AgreementSoourceIP", typeof(string));
                legalEntityTable.Columns.Add("CustomerServiceEmail", typeof(string));
                legalEntityTable.Columns.Add("CustomerServicePhoneNumber", typeof(string));
                legalEntityTable.Columns.Add("Active", typeof(bool));
                legalEntityTable.Columns.Add("TimeZone", typeof(string));
                legalEntityTable.Columns.Add("CreateTime", typeof(DateTimeOffset));
                legalEntityTable.Columns.Add("RiskToken", typeof(string));
                legalEntityTable.Columns.Add("DBA", typeof(string));
                legalEntityTable.Columns.Add("YearOfFormation", typeof(string));
                legalEntityTable.Columns.Add("DateOfFormation", typeof(string));


                return legalEntityTable;
            }
        }
    }
}

控制器

    using Microsoft.AspNetCore.Hosting;
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.Extensions.Logging;
    using OfficeOpenXml;
    using ppms_excel.Models;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Diagnostics;
    using System.Linq;
    using System.Threading.Tasks;
    using System.IO;
    using Excel = Microsoft.Office.Interop.Excel;
    
    
    namespace ppms_excel.Controllers
    {
        public class LoadDataTableController1 : Controller
        {
            public IActionResult Index()
            {
    
                //var x=new PODataTable().LDataTable.TableName;
                
                
                ExcelPackage ld = new ExcelPackage();
                
    
    
                ExcelWorksheet worksheet = ld.Workbook.Worksheets.FirstOrDefault();
                if (worksheet == null)
                {
                    //return or alert message here
                }
                else
                {
    
                    
    
                    var colcount= worksheet.Dimension.Columns;
                    string[] coltitle = new string[colcount];
                    for (int i = 1; i <= colcount; i++)
                    {
                        coltitle[i - 1] = (string) worksheet.Cells[1, i].Value;
                    }
    
    
    
    
                       //read excel file data and add data in  model.StaffInfoViewModel.StaffList
                           var rowCount = worksheet.Dimension.Rows;
                           var colCount = worksheet.Dimension.Columns;
                           **PODataTable Le = new PODataTable().LDataTable;**
    
    
                  
                }
                return View();
            }
        }
    }

这是在控制器中实例化数据表的正确方法吗?PODataTable Le = new PODataTable().LDataTable;

谢谢

标签: c#model-view-controllerdatatables

解决方案


当然,有很多方法可以解决这个问题。通常,将控制器逻辑与模型或实用程序逻辑分开总是一个好主意,就像您所做的那样。对 的引用ppms_excel.Models将允许您像拥有它一样使用该类。

一般来说,我会小心使用属性来构建和传回更复杂的对象。这可能会导致性能下降,因为每次都会重新创建对象。如果可能,将构建的对象存储在实用程序类的局部变量中,然后将其返回。这将防止不必要的重新创建(如果每次都相同)。


推荐阅读