Reputation: 478
I need to move this object around the application without losing it. At first, I just used a static class to do it, which was simple, fast and effective. Though, It's been brought to my attention that a static object can be accessed by anyone from anywhere. So, as that's a serious security risk, I've been trying to find another way do it. I've made an ajax request to do what needed to be done in the code behind (which, in this case, would be to book a new room in a hotel). All good. The thing is, I want to bring it back again to the client-side, to the model or something, to have it present for when I need to book another room. As of now, I click the "book" button and it adds a room but as soon as it goes back to the client-side the object is lost. So, I serialized it on the server-side and returned it to the server-side. But then I can't access it in the razor code.
I don't know if this can be done (this way, at least). But there has to be another way, since many websites have to make a sale, for instance, and switch pages and keep all the information. If possible, I want to avoid having to go to the database all the time to do this. I want to keep this in memory.
So, to show the code:
<button type="reset" class="btn btn-info center-block" onclick='BookRoom(
@Html.Raw(Json.Serialize(Model)),
"@Repository.RoomTypeList[i].UUID",
"@Repository.RoomTypeList[i].PossibleRateTypes[j].UUID",
"checkIn",
"checkOut");'>
Book
</button>
<script type="text/javascript">
function AjaxRooms(URL, method, contentType, request, dataType, successRespMsg, successRespObj) {
$.ajax({
url: URL,
method: method,
contentType: contentType,
data: JSON.stringify(request),
dataType: dataType,
success: function (resp) {
debugger
alert(resp[successRespMsg]);
var aux = JSON.parse(resp[successRespObj]);
// Here is where I want to access the object from javascript to razor, in order to keep it in memory
},
error: function (error) {
alert(error);
}
})
}
function BookRoom(model, roomUUID, rateUUID, checkIn, checkOut) {
AjaxRooms('Home/Book?ro=' + roomUUID + '&ra=' + rateUUID + '&ci=' + document.getElementById(checkIn).value + '&co=' + document.getElementById(checkOut).value,
'post', 'application/ json', model, 'json', 'msg', 'obj');
}
</script>
public class Reservation
{
public DateTime CheckIn { get; set; }
public DateTime CheckOut { get; set; }
public List<RoomType> BookedRooms { get; set; } = new List<RoomType>();
}
public IActionResult Book([FromBody]Reservation reservation)
{
var roomUUID = HttpContext.Request.Query["ro"].ToString();
var rateUUID = HttpContext.Request.Query["ra"].ToString();
var checkIn = HttpContext.Request.Query["ci"].ToString();
var checkOut = HttpContext.Request.Query["co"].ToString();
try
{
if (checkIn == "" || checkOut == "")
return Json(new { msg = "You have to select a Check In and a Check Out date.", obj = JsonConvert.SerializeObject(reservation) });
else if (roomUUID != "" || rateUUID != "")
{
// Adds a new room (with the specified rate) to the list
var roomType = Repository.DeepClone(Repository.RoomTypeList)
.FirstOrDefault(r => r.UUID == roomUUID);
roomType.PossibleRateTypes = new List<RateType>() { roomType.PossibleRateTypes.FirstOrDefault(rt => rt.UUID == rateUUID) };
reservation.BookedRooms.Add(roomType);
reservation.CheckIn = Convert.ToDateTime(checkIn);
reservation.CheckOut = Convert.ToDateTime(checkOut);
return Json(new { msg = "Added to cart.", obj = JsonConvert.SerializeObject(reservation) });
}
else
return Json(new { msg = "We couldn't add it to cart. Please try again.", obj = JsonConvert.SerializeObject(reservation) });
}
catch (Exception ex)
{
return Json(new { msg = "An error has occurred. Please try again later or contact and administrator.", obj = JsonConvert.SerializeObject(reservation) });
}
}
I've been doing it like this but you can propose another, better way, to do it. I'm all ears.
Upvotes: 0
Views: 744
Reputation: 610
Have you considered using local/session storage in the browser? That would be one way to keep the data in memory when the user jumps from page to page on your site.
Upvotes: 1