javascript 如何传递带其他参数的Jsonstringfy对象

kmbjn2e3  于 2023-02-21  发布在  Java
关注(0)|答案(1)|浏览(122)

我想用json stringify()传递更多的参数,但是当传递其他变量时,会得到空值。请让我知道传递更多变量的正确方法。

我试过这个

data: {model:JSON.stringify(arr), buildingid ,shopid,post},
$("#btnArchive").click(function () {
    var Buildingid = $("#BuildingId").val();
    var shopid = $("#ShopId").val();
    var Post = $("#SelectedValue").val();

    var arr = [];
    debugger;
    //Loop through the Table rows and build a JSON array.
    var customers = new Array();
    $(".tblSavingCollChk:checked").each(function () {
        var row = $(this).attr("chkid");
        alert(row);
        debugger;
        //customers.push(row);
        arr.push({ Id: row });
    });

    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "/Archive/UpdateStatus",
        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) {},
    });
});

控制器操作公共操作结果更新状态([FromBody] ArchiveViewModel []模型,字符串建筑ID,字符串商店ID,字符串发布)//列表值){}

fafcakar

fafcakar1#

如果你想传递更多的变量,我有一个工作演示如下,你可以参考它:
删除[Frombody]和数据类型、内容类型
在控制器中:

public class jsonPostController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }
        [HttpPost]
        public IActionResult GetData(Author jsonInput ,string cc, string buildingid, string shopid)
        {
            return View();
        }
    }

索引视图:

<input type="button" id="btnGet" value="submit" />

<input id="BuildingId" value="aa" type="hidden" />
<input id="ShopId" value="aaa" type="hidden" />

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>

<script>
  $(function () {
        $("#btnGet").click(function () 
{  
    // Initialization  
    var jsonInput = {Id: 23, Name: "John" };
            var buildingid = $("#BuildingId").val();
            var shopid = $("#ShopId").val();
    $.ajax(  
        {  
            type: 'POST',                 
            url: '/jsonPost/GetData',                   
            data: {
                        jsonInput: jsonInput,
                        cc: "cc", buildingid: buildingid, shopid: shopid
                    },       
        });          
});
    });

</script>

作者:

public class Author
    {
        public int Id { get; set; }
        public string Name { get; set; }      
    }

结果:

相关问题