如何将 json 对象传递给 mvc 控制器
我希望能够将 2 个参数传递给我的控制器。一个 id 和一个 object[]。
这是我的控制器:
[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 和对象感到困惑。这是我的ajax调用:
$.ajax({
url: '/Member/SaveCoordinates/@Model.Id',
type: "POST",
data: window.image.pinpoints,
success: function (data) {
alert(data);
},
error: function () {
alert("error");
}
});
return false;
});
});
@Model.Id应该是我想要传递到控制器的“Id”参数中的id,而window.image.pinpoints是我想要通过“pFrame”传递的对象目的。我怎样才能成功地做到这一点,以便两个参数都能正确传递?我想可能和我写ajax jquery函数的方式有关系。
I want to be able pass 2 arguments to my controller. An id and an object[].
Here is my 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;
}
I know the way i wrote the method may not be correct, but im just confused on how I can pass both a string id and the object in an ajax call. Here is my ajax call:
$.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. How can i successfully do this so both parameters get passed in correctly? I think it may have something to do with the way I write the ajax jquery function.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个
Try this
在你的ajax帖子中
in your ajax post