tes*_*icg 1 asp.net-mvc jquery
我有以下结构:
HomeControler中有一个方法:
public JsonResult GetDbData()
{
WebService1SoapClient client = new WebService1SoapClient();
List<Person> lstPersons = client.GetPersons();
return Json(lstPersons, JsonRequestBehavior.AllowGet);
}
Run Code Online (Sandbox Code Playgroud)
我想以这种方式使用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);
}
});
});
Run Code Online (Sandbox Code Playgroud)
但是,它不起作用.
我是jQuery的新手,需要某种表或模板或类似转发器的东西,可以显示列表或表结构.
Person类看起来如下:
public class Person
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我需要在Html表中显示人员列表.
为什么要设置的内容div3用.html?
success: function (data) {
$("#div3").html('');
var div3Content = '';
for(var i = 0; i < data.length; i++)
{
div3Content += '<p>' + data[i].Name + '</p>'; // if Name is property of your Person object
}
$("#div3").append(div3Content);
},
Run Code Online (Sandbox Code Playgroud)
这样一来,Person名字将被添加到div3作为<p>标签
| 归档时间: |
|
| 查看次数: |
16618 次 |
| 最近记录: |