gpt4 book ai didi

c# - 使用 jquery/Ajax 使用对象模型渲染部分 View

转载 作者:太空宇宙 更新时间:2023-11-03 10:59:23 24 4
gpt4 key购买 nike

我现在的代码是这样的

<!-- some html -->
{
// some code
@Html.Partial("~/Views/AdminUser/Main.cshtml", Model.AdminUserModel)
}

但是,我需要它来代替 ajax 调用。如何在调用中包含模型的情况下执行 jquery ajax 调用?

最佳答案

我是如何做到的是一个传递 id 的 ajax 调用:

$.ajax({
url: "@(Url.Action("Action", "Controller", new { id = "----" }))/".replace("----", id),
type: "POST",
cache: false,
async: true,
success: function (result) {
$(".Class").html(result);
}
});

然后在你的 Controller 中设置 Action

public PartialViewResult Action(string id)
{
//Build your model
return PartialView("_PartialName", model);
}

如果您确实需要通过 ajax 将模型传递给 Controller ​​,如果您创建一个与模型具有相同字段的 jquery 对象并将其字符串化并传递它,它将正确传递。

var toSend = {};
toSend.ID = id;
toSend.Name = name;

等等,然后在ajax调用中

data: JSON.stringify(toSend),

关于c# - 使用 jquery/Ajax 使用对象模型渲染部分 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18256047/

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