Angular 6/C#/MVC 动态 return Json 来自控制器的结果 Json Array of Array
Angular 6/C#/MVC Dynamically return JsonResult from controller in form of Json Array of Array
我试图在 Angular 6 应用程序中将一些数据集导出到 Excel。为此,我正在使用 XLSX 和文件保存,如前所述:
https://medium.com/@madhavmahesh/exporting-an-excel-file-in-angular-927756ac9857
问题是字段可能会不时更改,所以我需要一些动态的东西(不想指定列名)。为此,我找到了一个很好的例子:
Binding Json data to a table in mvc 4
但是,我没有得到正确格式的数据。我需要像数组数组这样的东西。
我假设 return 类型在那种情况下应该是 async Task<JsonResult>
但不确定如何让它工作。
我附上了两张图片 - 我得到的结果
和预期结果
这是引用示例中的代码。对 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;
}
}
请多指教!
P.S.
此技术使用 Dapper。它 return 所需的数据(数组的数组)格式,但在对象中我们必须指定列名,这将是硬编码,因此,我不能使用
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 控制器调用
[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
};
}
您应该 return 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;
}
我试图在 Angular 6 应用程序中将一些数据集导出到 Excel。为此,我正在使用 XLSX 和文件保存,如前所述: https://medium.com/@madhavmahesh/exporting-an-excel-file-in-angular-927756ac9857
问题是字段可能会不时更改,所以我需要一些动态的东西(不想指定列名)。为此,我找到了一个很好的例子: Binding Json data to a table in mvc 4
但是,我没有得到正确格式的数据。我需要像数组数组这样的东西。
我假设 return 类型在那种情况下应该是 async Task<JsonResult>
但不确定如何让它工作。
我附上了两张图片 - 我得到的结果
和预期结果
这是引用示例中的代码。对 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;
}
}
请多指教!
P.S.
此技术使用 Dapper。它 return 所需的数据(数组的数组)格式,但在对象中我们必须指定列名,这将是硬编码,因此,我不能使用
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 控制器调用
[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
};
}
您应该 return 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;
}