gpt4 book ai didi

c# - Angular 6/C#/MVC 以 Json Array of Array 的形式从 Controller 动态返回 JsonResult

转载 作者:太空宇宙 更新时间:2023-11-03 12:07:15 25 4
gpt4 key购买 nike

我正在尝试在 Angular 6 应用程序中将大量数据集导出到 Excel。为此,我正在使用 XLSX 和文件保存,如前所述: https://medium.com/@madhavmahesh/exporting-an-excel-file-in-angular-927756ac9857

问题是字段可能会不时更改,所以我需要一些动态的东西(不想指定列名)。为此,我找到了一个很好的例子: Binding Json data to a table in mvc 4

但是,我没有得到正确格式的数据。我需要像数组数组这样的东西。我假设返回类型应该是 async Task<JsonResult>在那种情况下,但不确定如何使其工作。我附上了两张图片 - 我得到的结果 resultObtained

和预期结果 ArrofArr

这是引用示例中的代码。对 GetMyData 的存储过程调用,使用 SqlAdapter,无需指定字段名称

 public JsonResult GetValue()
{
JsonResult json = new JsonResult();
DataSet ds = GetMyData();
/*LoadDoctordetailsNew is method where i get data from database and convert
to dataset.It returns a dataset*/
string returnData = GetJson(ds.Tables[0]);
json.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
json.Data = returnData;
return json;
}

public static string GetJson(DataTable dt)
{
System.Web.Script.Serialization.JavaScriptSerializer serializer =
new System.Web.Script.Serialization.JavaScriptSerializer();
List<Dictionary<string, object>> rows =
new List<Dictionary<string, object>>();
Dictionary<string, object> row = null;

foreach (DataRow dr in dt.Rows)
{
row = new Dictionary<string, object>();
foreach (DataColumn col in dt.Columns)
{
row.Add(col.ColumnName, dr[col]);
}
rows.Add(row);
}
return serializer.Serialize(rows);
}

public static DataSet GetMyData()
{
try
{
using (SqlConnection connection = Connection.GetConnection())
{
SqlDataAdapter da = new SqlDataAdapter("dbo.MySQLStoredproc", connection);
da.SelectCommand.CommandType = CommandType.StoredProcedure;
DataSet ds = new DataSet();
da.Fill(ds);
return ds;
}
}
catch (Exception ex)
{
throw;
}
}

请指导!

附言

此技术使用 Dapper。它按需要(数组的数组)格式返回数据,但在对象中我们必须指定列名,这将是硬编码,因此,我不能使用

public static IEnumerable<Object> GetMyData()
{

var strQuery = @"[dbo].[Myproc]";
IEnumerable< Object > items = new List<Object>(0);
var p = new DynamicParameters();

using (SqlConnection con = Connection.GetConnection())
{
items = con.Query<Object>(strQuery, param: p, commandTimeout: 120, commandType: System.Data.CommandType.StoredProcedure);
}
return items;
}

从 MVC Controller 调用

[HttpPost]
public async Task<JsonResult> SelectMyData()
{
var task = Task.Run(() => Request.GetMyData());

var retData = await task;
return new JsonResult
{
ContentType = "application/json",
Data = retData,
JsonRequestBehavior = JsonRequestBehavior.AllowGet,
MaxJsonLength = int.MaxValue
};
}

最佳答案

您应该返回 List<Dictionary<string, object>>而不是 string .你不需要 Serialize JsonResult 将处理的数据

public JsonResult GetValue()
{
JsonResult json = new JsonResult();
DataSet ds = GetMyData();
/*LoadDoctordetailsNew is method where i get data from database and convert
to dataset.It returns a dataset*/
json.Data = GetJson(ds.Tables[0]);
json.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
return json;
}

public static List<Dictionary<string, object>> GetJson(DataTable dt)
{
List<Dictionary<string, object>> rows =
new List<Dictionary<string, object>>();
Dictionary<string, object> row = null;

foreach (DataRow dr in dt.Rows)
{
row = new Dictionary<string, object>();
foreach (DataColumn col in dt.Columns)
{
row.Add(col.ColumnName, dr[col]);
}
rows.Add(row);
}
return rows;
}

关于c# - Angular 6/C#/MVC 以 Json Array of Array 的形式从 Controller 动态返回 JsonResult,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54319766/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com