Your problem is the onclick event handler here:
<img id="@item.treasureID" src="@imageSource" onclick="return MakeSure(@item.treasureID)" />
In the MakeSure() function, you are not returning anything. Thus, you have two options, change the MakeSure() function to return false at the end, or change the onclick event to return false after the first function call in the image element, for example, onclick="MakeSure(@item.TreasureID); return false;"
Also you need to remove window.location.reload(); from the MakeSure() function.
Side note, it looks like you are mixing your DbContext in your view, if so, this is very bad practice. You must put your access to the data at some service level, which serves as an intermediary between the view and your data.
Update
Well, after reading my question several times, I understand your problem. The problem is your second part of the code.
List<The_Factory_Chante.Models.Learner_Treasure> lern; using (The_Factory_Chante.Models.The_FactoryDBContext db2 = new The_Factory_Chante.Models.The_FactoryDBContext()) { string imageSource = ""; lern = db2.Learner_Treasure.ToList(); if (lern != null) { foreach (var item in lern) { if (item.learnerID == UserInfo.ID) { byte[] bytes = db2.Treasures.FirstOrDefault(au => au.treasureID == item.treasureID).itemImage; string imageBase = Convert.ToBase64String(bytes); imageSource = string.Format("data:image/gif;base64,{0}", imageBase); <img id="@item.treasureID" src="@imageSource"/> } } } }
This calls the database and gets a list of Learner_Treasure objects that you use for display. Server code runs after each page request, this is exactly what happens. It will not be updated asynchronously without a request to the server.
You need to implement an ajax request to display the last Learner_Treasure list in the view. Again, this boils down to the first note I gave, the reason is that you mix your dbcontext with your view and expect it to be updated on the fly. If you implement a layer that serves your data view (controller), you can call it asynchronously and refresh the page without reloading.
For example, you could write a call in your controller to get one LearnerTreasure element in json.
[HttpGet] public ActionResult GetLearnerTreasureItem(int Id) { using (The_Factory_Chante.Models.The_FactoryDBContext db2 = new The_Factory_Chante.Models.The_FactoryDBContext()) { learnerTreasureItem = db2.Learner_Treasure.FirstOrDefault(x => x.Id == Id); return Json(new { image = Convert.ToBase64String(learnerTreasureItem.itemImage), treasureId = learnerTreasureItem.TreasureID }, JsonRequestBehavior.AllowGet); } }
And then name it with ajax in your view, as you did with the update.
$.ajax({ cache: false, type: "GET", url: '/YOURCONTROLLERNAME/GetLearnerTreasureItem?id=1', contentType: 'application/json', dataType: "json", success: function (data) { //build image element var image = '<img id="' + data.treasureId + '" src="data:image/jpg;base64,' + data.image + '"'; //add the image to where you need it. $(image).appendTo("body"); }, error: function (xhr) { alert("Error occurred while loading the image."); } });
Hope this helps.