First of, I got some help from nice people yesterday helping me decide on what approach to take: http://stackoverflow.com/questions/24832038/pass-newly-created-object-to-partial-view/24834225#24834225
Summary: I got some Ajax calling a method that creates an object. I would like to use the Ajax success-message to replace the content in a div with my new object. This is the Ajax:
$("#btnSaves").click(function () {
var image = document.getElementById("canvas").toDataURL("image/png");
image = image.replace('data:image/png;base64,', '');
$.ajax({
type: 'POST',
url: "../../Home/UploadImageS", //THIS METHOD CREATES AN OBJECT
data: '{ "imageData" : "' + image + '" }',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function (?) {
HERE I NEED CODE THAT RENDERS A PARTIAL VIEW PASSING THE OBJECT AS A PARAMETER.
}
});
});
This is the controller, I'm not sure what its return value should be:
public ActionResult UploadImageS(string imageData)
{
string fileNameWitPath = path + DateTime.Now.ToString().Replace("/", "-").Replace(" ", "- ").Replace(":", "") + ".png";
using (FileStream fs = new FileStream(fileNameWitPath, FileMode.Create))
{
using (BinaryWriter bw = new BinaryWriter(fs))
{
byte[] data = Convert.FromBase64String(imageData);
bw.Write(data);
bw.Close();
}
}
string link = UploadImage(fileNameWitPath);
JObject o = JObject.Parse(link);
string name = (string)o.SelectToken("data.link");
var db = new ArtContext();
var nyArt = new ArtWork()
{
ArtLink = name,
};
db.ArtWorks.Add(nyArt);
db.SaveChanges();
Return ??
}
}
I'm not that comfortable working with Ajax so I would also appreciate some explanation if possible.
This is what my method returns:
return View("TestView",nyArt);
The testview, created without layout:
@model Heroz.Models.ArtWork
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>TestView</title>
</head>
<body>
<div>
<img src="@Model.ArtLink"/>
</div>
</body>
</html>
Now, when I put a brekpoint at:
return View("TestView",nyArt);
I does not step into the ""TestView", It goes directly to the _Layout.