Search code examples
javascript.netasp.net-coremodel-view-controller

How to Pass Jsonstringfy object with other parameters


I want pass more parameter with json stringify() but when passing other variable getting null value. please let me know right way of passing more variable .

I have tried this

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) {},
    });
});

Controller action public ActionResult UpdateStatus([FromBody] ArchiveViewModel[] model,string buildingid, string shopid, string Post)//List values) {}


Solution

  • If you want to pass more variable,I have a work demo like below, you can refer to it:

    remove [Frombody] and dataType,contentType

    In the controller:

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

    Index 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 arr = new Array();
                var  ListOfAuthors = { Id: 23, Name: "John" };
                arr.push(ListOfAuthors);
                var jsonInput = {};
                jsonInput.Authors = arr;
                var buildingid = $("#BuildingId").val();
                var shopid = $("#ShopId").val();
        $.ajax(  
            {  
                type: 'POST',                 
                url: '/jsonPost/GetData',                   
                data: {
                            jsonInput: jsonInput,
                            cc: "cc", buildingid: buildingid, shopid: shopid
                        },       
            });          
    });
        });
    
    </script>
    

    Author:

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

    Update

    If you want to pass array, we can add a new model , ajax change as above:

    AuthorVm:

    public class AuthorVm
        {
            public List<Author> Authors { get; set; }   
        }
    

    Result:

    enter image description here