我需要一个关于如何使用Ajax获取我的MVC Json结果并将其填充到我的视图表中的想法,
这是我的Json结果。
这是我的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>
and this is my Script
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让其正常工作,谢谢!
注意:请勿删除html标签。
contact.Name
和contact.MobileNumber
更改为this.Name
和this.MobileNumber
。 - dklingman