Reputation: 12437
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.
Upvotes: 4
Views: 1367
Reputation: 21366
in your ajax post
data: { pFrame: JSON.stringify(window.image.pinpoints), Id: modelId }
Upvotes: 1
Reputation: 32576
Try this
data: {pFrame : JSON.stringify(window.image.pinpoints)},
Upvotes: 2