【发布时间】:2015-09-22 23:43:41
【问题描述】:
我有以下结构:
- WebService - 从数据库中获取数据并返回列表 人
- ASP.NET MVC 应用程序 - 使用 1) 中的 Web 服务。
HomeControler 中有一个方法:
public JsonResult GetDbData()
{
WebService1SoapClient client = new WebService1SoapClient();
List<Person> lstPersons = client.GetPersons();
return Json(lstPersons, JsonRequestBehavior.AllowGet);
}
我想以这种方式使用 jQuery Ajax 调用在 Index.cshtml 视图中显示人员列表:
<div>
<div id="div3">Database data.</div>
</div>
<input id="btnGetDBData" type="button" value="GetDBData" />
$('#btnGetDBData').click(function () {
$.ajax({
url: "/Home/GetDbData",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
$("#div3").html(data);
},
error: function (xhr, status) {
alert(status);
}
});
});
但是,它不起作用。
我是 jQuery 新手,需要某种表格或模板,或者类似可以显示列表或表格结构的中继器。
Person 类如下所示:
public class Person
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
我需要在 Html 表格中显示人员列表。
【问题讨论】:
-
您正在返回一个数组,您应该尝试循环或使用数组索引访问它 => stackoverflow.com/questions/15848183/…
-
制作部分视图,显示列表并从您的操作中返回。
-
一切都应该在 JavaScript (jQuery) 中。
标签: asp.net-mvc jquery