You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Ajax请求中同时传递JSON.stringify对象与其他参数?

解决JSON.stringify传参时额外参数为null的问题

问题原因

当你设置contentType: "application/json; charset=utf-8"时,ASP.NET MVC会从请求体的整个JSON对象中解析参数。你之前的写法要么只传了数组,要么错误地混合了序列化字符串和其他变量,导致后端无法正确绑定单独的buildingid、shopid、Post参数,最终出现null值。

正确方案一:使用统一请求模型(推荐)

这种方式更规范,适合复杂参数场景:

  1. 后端定义统一请求ViewModel
public class ArchiveUpdateRequest
{
    public ArchiveViewModel[] Model { get; set; }
    public string BuildingId { get; set; }
    public string ShopId { get; set; }
    public string Post { get; set; }
}
  1. 修改Controller Action
public ActionResult UpdateStatus([FromBody] ArchiveUpdateRequest request)
{
    // 直接使用request.Model、request.BuildingId、request.ShopId、request.Post
}
  1. 前端构造完整请求对象并序列化
    修改Ajax的data部分:
$("#btnArchive").click(function () {
    var Buildingid = $("#BuildingId").val();
    var shopid = $("#ShopId").val();
    var Post = $("#SelectedValue").val();

    var arr = [];
    $(" .tblSavingCollChk:checked").each(function () {
        var row = $(this).attr("chkid");
        arr.push({ Id: row });
    });

    // 构造包含所有参数的请求对象
    var requestData = {
        Model: arr,
        BuildingId: Buildingid,
        ShopId: shopid,
        Post: Post
    };

    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "/Archive/UpdateStatus",
        data: JSON.stringify(requestData), // 序列化整个对象
        dataType: "json",
        success: function (r) {
            if (r == true) {
                alert("Record Updated successfully");
                document.location = '@Url.Action("ArchiveList","Archive")';
            }
        },
        error: function (err) {},
    });
});

正确方案二:将额外参数放到URL查询字符串(适合简单参数)

如果不想修改后端ViewModel,可以把额外参数拼到请求URL上,ASP.NET MVC会自动解析查询字符串中的参数:

  1. 修改前端Ajax的URL
$("#btnArchive").click(function () {
    var Buildingid = $("#BuildingId").val();
    var shopid = $("#ShopId").val();
    var Post = $("#SelectedValue").val();

    var arr = [];
    $(" .tblSavingCollChk:checked").each(function () {
        var row = $(this).attr("chkid");
        arr.push({ Id: row });
    });

    // 拼接查询字符串,注意参数名要和后端Action的参数名一致(大小写对应)
    var url = "/Archive/UpdateStatus?buildingid=" + encodeURIComponent(Buildingid) 
            + "&shopid=" + encodeURIComponent(shopid) 
            + "&Post=" + encodeURIComponent(Post);

    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: url,
        data: JSON.stringify(arr), // 依然序列化数组
        dataType: "json",
        success: function (r) {
            if (r == true) {
                alert("Record Updated successfully");
                document.location = '@Url.Action("ArchiveList","Archive")';
            }
        },
        error: function (err) {},
    });
});
  1. 后端Action保持不变
    确保参数名和URL中的参数名完全一致(包括大小写,比如后端的buildingid对应URL里的buildingid):
public ActionResult UpdateStatus([FromBody] ArchiveViewModel[] model,string buildingid, string shopid, string Post)
{}

你之前写法的错误点

data: {model:JSON.stringify(arr), buildingid ,shopid,post}, 存在两个问题:

  • 当contentType设为application/json时,data必须是JSON字符串,而不是JS对象;
  • 即使序列化这个对象,后端的[FromBody] ArchiveViewModel[] model无法绑定到一个字符串类型的model字段,同时额外参数也不在请求体的JSON结构中,导致后端无法识别。

内容的提问来源于stack exchange,提问作者Jaya acharya

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 11:45:24