【发布时间】:2017-12-15 16:59:17
【问题描述】:
我一直试图在谷歌上找到答案,但没有任何结果。我的问题是如何在我的视图中操作我的 Json 数据 (List<string>)?例如,我想显示一个 div 中返回的所有字符串。
这是我目前卡在的地方:
控制器
[HttpPost]
public async Task<ActionResult> RetournerOP(int OF)
{
List<string> ops = new List<string>();
Task verif = Task.Run(() =>
{
try
{
connection.Open();
string sqlQuery = "SELECT Operation from ZZ where ordre = " + OF;
SqlCommand command = new SqlCommand(sqlQuery, connection);
using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
ops.Add(Convert.ToString(reader["Operation"]));
}
}
}
catch (Exception) { }
finally { connection.Close(); }
});
await verif;
return Json(ops);
}
查看
function retournerOp() {
$.ajax({
url: '@Url.Action("RetournerOp", "Home", new { area = "Ajout" })',
data: {OF: document.getElementById("NumOf").value},
type: 'POST',
dataType: 'JSON',
cache: false,
success: function (data) {
//How can I manipulate my data returned?
}
});
}
【问题讨论】:
-
您是否尝试循环使用
data并使用它?$.each(data,function(a,b){ //do something }); -
您有两个选择 - 要么将预先填充的视图返回到您的 ajax 响应并将其注入到您的 HTML 中,要么使用某种数据绑定库在客户端使用 html 模板修补您的 JSON 集合-一边。
-
你能展示你的html模板吗?
-
请使用参数化查询,而不是串联字符串。
-
@Stephen 我会继续阅读的!
标签: c# html asp.net json asp.net-mvc