[英]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.