Hi I have this as one of my controllers:
[HttpPost]
public JsonResult GetPinPoints(string Id)
{
Frames rslt = null;
string connString = ConfigurationManager.ConnectionStrings["MongoConnStringNew"].ToString();
MongoUrl murl = new MongoUrl(connString);
MongoServer mgconf = new MongoServer(murl);
try
{
mgconf.Connect();
MongoDatabase frmlydb = mgconf.GetDatabase("framely");
MongoCollection<Frames> collection = frmlydb.GetCollection<Frames>("Frames");
ObjectId oid = new ObjectId(Id);
Frames frms = collection.FindOne(Query.EQ("_id", oid));
if (frms != null)
{
rslt = frms;
}
}
catch (Exception ex)
{
}
finally
{
mgconf.Disconnect();
}
return Json(rslt.CoordinatesObj.ToJson());
}
The mongo object looks like this:
{"MetaTagsObj":{"Meta1":"my fam","Meta2":"lololo","Meta3":"lulz"},"PictureID":"http://framely.s3.amazonaws.com/0b7a9a72-c61b-4dec-a814-40b003072e31.jpg","UserID":"1","CoordinatesObj":[{"Position":1,"Top":182,"Left":20,"Height":73,"Width":90},{"Position":2,"Top":69,"Left":103,"Height":98,"Width":1...
I use an ajax jquery function to call the controller that looks like this:
$("#mybutton").click(function(){
$.ajax({
url: '/Member/GetPinPoints',
type: "POST",
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function(data) {
alert(data);
},
error: function() {
alert("error");
}
});
return false;
});
I dont think I am doing this right, I think it has to do with the way I return the json object. I keep getting this error:
{“Object reference not set to an instance of an object.”}
right where I am returning the Json object.
The
return Json(myObject)statement should take an object that’ll be serialised to JSON then returned to the browser as a string, but by callingToJson()therslt.CoordinatesObjobject will be serialised twice.It’s also possible
CoordinatesObjisn’t being deserislised properly, so it’s throwing an exception becauseToJson()is called on a null object.The
Framesclass should look something like this to handle deserialising theCoordinatesObjarray: