gpt4 book ai didi

javascript - 从razor中的javascript访问对象

转载 作者:行者123 更新时间:2023-12-03 02:52:15 26 4
gpt4 key购买 nike

我需要在应用程序中移动这个对象而不丢失它。一开始我只是用静态类来做,简单、快速、有效。不过,我注意到静态对象可以被任何地方的任何人访问。因此,由于这是一个严重的安全风险,我一直在尝试寻找另一种方法。我已经发出了一个ajax请求来执行后面的代码中需要完成的操作(在本例中,将是预订酒店的新房间)。都好。问题是,我想将其再次带回客户端、模型或其他东西,以便在我需要预订另一个房间时将其呈现出来。截至目前,我单击“预订”按钮,它会添加一个房间,但一旦返回客户端,该对象就会丢失。所以,我在服务器端序列化它并返回给服务器端。但是我无法在 razor 代码中访问它。

我不知道这是否可以完成(至少可以这样)。但必须有另一种方法,因为许多网站必须进行销售,例如,切换页面并保留所有信息。如果可能的话,我想避免一直访问数据库来执行此操作。我想记住这一点。

因此,显示代码:

<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) });
}
}

我一直这样做,但你可以提出另一种更好的方法来做到这一点。我洗耳恭听。

最佳答案

您是否考虑过使用local/session存储在浏览器中?这是当用户在网站上从一个页面跳转到另一个页面时将数据保留在内存中的一种方法。

关于javascript - 从razor中的javascript访问对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47798316/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com