簡體   English   中英

如何將 json object 傳遞給 mvc controller

[英]How to pass a json object to a mvc controller

我希望能夠將 2 個 arguments 傳遞給我的 controller。 一個 id 和一個對象 []。

這是我的 controller:

[HttpPost]
        public string SaveCoordinates(string Id, object[] pFrame) 
        {

            string rslt = "ERROR";
            if (pFrame != null)
            {
                try
                {
                    List<Coordinates> pList = new List<Coordinates>();
                    for (int i = 0; i < pFrame.Length; i++)
                    {
                        Dictionary<string, object> kvps = (Dictionary<string, object>)pFrame[i];
                        pList.Add(new Coordinates
                        {
                            Position = Convert.ToInt32(kvps["position"]),
                            Height = Convert.ToInt32(kvps["height"]),
                            Width = Convert.ToInt32(kvps["width"]),
                            Top = Convert.ToInt32(kvps["top"]),
                            Left = Convert.ToInt32(kvps["left"])
                        });
                    }

                    MongoDBSaveOneFrameCoordinates(Id, pList);
                    rslt = "SUCCESS";
                }
                catch (Exception ex)
                {
                }
                //foreach (Coordinates c in pFrame)
                //{
                //    string asdf;
                //}
            }
            return rslt;
        }

我知道我編寫該方法的方式可能不正確,但我只是對如何在 ajax 調用中同時傳遞字符串 id 和 object 感到困惑。 這是我的 ajax 電話:

$.ajax({
                    url: '/Member/SaveCoordinates/@Model.Id',
                    type: "POST",
                    data: window.image.pinpoints,
                    success: function (data) {

                       alert(data);
                    },
                    error: function () {

                        alert("error");
                    }
                });


                return false;
            });
      });

The @Model.Id is suppose to be the id that I want to pass into the "Id" parameter of my controller, and the window.image.pinpoints is the object I wanna pass through the "pFrame" object. 我怎樣才能成功地做到這一點,以便正確傳遞兩個參數? 我認為這可能與我編寫 ajax jquery function 的方式有關。

嘗試這個

data: {pFrame : JSON.stringify(window.image.pinpoints)},

在您的 ajax 帖子中

data: { pFrame: JSON.stringify(window.image.pinpoints), Id: modelId }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM