sách gpt4 ăn đã đi

javascript - 如何使用 Ajax 获取 MVC Json 结果并填充到表中

In lại 作者:数据小太阳 更新时间:2023-10-29 04:40:33 24 4
mua khóa gpt4 giày nike

我需要了解如何获取我的 MVC Json 结果并使用 Ajax 将其填充到我的 View 表中,

这是我的 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);

}

请问我该如何存档?

其次,我的表格有复选框,我可以选择手机号码并将它们填充到列表框中

这是我的表格 View


<đầu>















Contact Name(s) Mobile Number(s)

这是我的脚本

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) {
$('', {
html: $('', {
html: contact.Name
}).after($('', {
html: contact.MobileNumber
}))
}).appendTo(tableBody);
});
},
error: function () { alert("error"); }
});

$('#getContacts').click(函数 () {

GetContact();

});

我需要一些帮助来了解如何使用 jQuery 和 AJAX 来实现这一点,因为我无法弄清楚问题是否来自表单,请非常感谢...

câu trả lời hay nhất

您可以尝试以下方法:

public JsonResult GetAllContacts()
{
var user = GetLoggedInUserID();
var contacts = _contactService.GetUserContacts(user).Select(x => new
{
Id = x.Id,
Name = x.Name,
MobileNumber = x.MobileNumber
}).ToList(); // <--- cast to list if GetUserContacts returns an IEnumerable
return Json(contacts, JsonRequestBehavior.AllowGet);
}

在您看来,将此 JSON 数据填充到网格中:

HTML


<đầu>











Contact Name(s) Mobile Number(s)

jQuery

function GetContact() {    
$.ajax({
url: "/Contact/GetAllContacts",
loại: "LẤY",
contentType: "application/json; charset=utf-8",
data: "{}",
dataType: "json",
success: function (data) {
var row = "";
$.each(data, function(index, item){
row+=""+item.Name+""+item.MobileNumber+"";
});
$("#contacts").html(row);
},
error: function (result) {
alert("Error");
}
});
}

$('#getContacts').click(function(){
GetContact();
});

biên tập:添加额外的要求,以将所选复选框中的手机号码填充到列表框

$("#add_recipient").click(function(e){
e.preventDefault();
$("#contacts input:checkbox:checked").map(function(){
var contact_number = $(this).closest('td').next('td').next('td').text();
var id = $(this).attr('id');
$('#recipientList').append('');
}).get();
});

Working Demo

关于javascript - 如何使用 Ajax 获取 MVC Json 结果并填充到表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24407108/

24 4 0
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress