gpt4 book ai didi

javascript - 从 Controller 发送两个参数到 Ajax |网络

转载 作者:行者123 更新时间:2023-12-02 21:47:06 25 4
gpt4 key购买 nike

我有一个问题是否可以将两个参数从 Controller 发送到 Ajax 进行查看?

我有两个存储数据的变量

我需要一个最佳解决方案

我尝试过:

Controller :

 [HttpGet]
public async Task<ActionResult> GetDepartmentAndManager(string userId)
{

var danePracownika = _uzytkownicyContext.Uzytkownicy
.Where(x => x.Id == IntIdCzlowieka)
.Select(s => new
{
UserDepartament = s.Departament,
UserManager = s.ManagerLogin

});


var userDetailsAU = _ecpContext.AC_Merge_V
.Where(x => x.Id == IntIdCzlowieka)
.Select(s => new
{
WymiarEtatu = s.WymiarEtatu,
NormaDzienna = s.NormaDzienna

});


return Json(danePracownika, userDetailsAU);
}


Ajax | javascript/ View :

  $.ajax({

url: "@Url.Action("GetDepartmentAndManager", "Uzytkownicy")",
type: "GET",
dataType: "json",
contentType: "application/json; charset=utf-8",
data: {"userId": JSON.stringify(userId)},
cache: false,
success: function (data, user) {
$("#Id_department")
.dxTextBox("instance")
.option("value", data[0].UserDepartament);
$("#Id_manager")
.dxTextBox("instance")
.option("value", data[0].UserManager);
}

});

最佳答案

将这些值作为对象的属性返回:

return Json(new { data = danePracownika, user = userDetailsAU });

然后在响应中它们将是结果的属性:

success: function (result) {
$("#Id_department")
.dxTextBox("instance")
.option("value", result.data[0].UserDepartament);
$("#Id_manager")
.dxTextBox("instance")
.option("value", result.data[0].UserManager);
// I assume you also want to use result.user somewhere...
}

关于javascript - 从 Controller 发送两个参数到 Ajax |网络,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60228645/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com