【发布时间】:2014-08-15 22:10:59
【问题描述】:
我需要了解如何获取我的 MVC Json 结果并使用 Ajax 将其填充到我的视图表中,
这是我的 json 结果
public JsonResult GetAllContacts()
{
var User = GetLoggedInUserID();
var getContact = _contactService.GetUserContacts(User).Select(x => new
{
Id = x.Id,
Name = x.Name,
MobileNumber = x.MobileNumber
});
return Json(getContact, JsonRequestBehavior.AllowGet);
}
请问我该如何存档?
其次,我的表有复选框,我可以选择手机号码并将它们填充到列表框中
这是我的表格视图
<table class="table table-striped table-hover table-bordered" id="contacts">
<thead>
<tr>
<th><input type="checkbox" name="chooseAllRecipient" id="chooseAllRecipient" /></th>
<th class="center">Contact Name(s)</th>
<th class="center">Mobile Number(s)</th>
</tr>
</thead>
<tbody>
<tr>
<td><input type="checkbox" name="chooseRecipient" class="my_chkBox"></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
这是我的脚本
function GetContact() {
$.ajax({
url: table.data('/Contact/GetAllContacts'),
type: 'GET',
contentType: 'application/json',
data: JSON.stringify(),
cache: false,
context: table,
success: function (contact) {
var tableBody = this.find('tbody');
tableBody.empty();
$.each(contact, function (index, contact) {
$('<tr/>', {
html: $('<td/>', {
html: contact.Name
}).after($('<td/>', {
html: contact.MobileNumber
}))
}).appendTo(tableBody);
});
},
error: function () { alert("error"); }
});
}
$('#getContacts').click(function () {
GetContact();
});
请我需要一些关于如何使用 jQuery 和 AJAX 的帮助,因为我不知道问题是否出现了,非常感谢你...
【问题讨论】:
-
您是否发现您使用联系人作为数据参数和联系人作为项目参数?它是相同的名称,它会导致功能混乱。改一下看看能不能解决。
-
正如@Fals 所说,将
contact.Name和contact.MobileNumber更改为this.Name和this.MobileNumber。 -
另一个问题是table是什么?我假设它是一个变量,但我猜你没有显示所有的 javascript 代码。
标签: javascript jquery ajax asp.net-mvc json