当查询MVC应用程序时,它将运行代码并带回相关数据,但是当有人使用系统的另一部分几秒钟后添加数据时会使被调用的代码带回不同的结果,因为原始用户是在同一会话中,不会重新查询数据库,就好像控制器正在返回缓存结果一样。如果他们注销并关闭浏览器并重新开始,他们将获得新的结果。
如何让它始终重新查询数据库?
cshtml代码:
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/bootstrap")
@RenderSection("scripts", required: false)
<script async type="text/javascript">
$(function () {
$("#SelecteZoneId").on("change", function () {
var zoneId = $(this).val();
var vrnUrl = "@Url.Action("GetVRNListForZone", "Check")?zoneId=" + zoneId;
$.get(vrnUrl, function (data) {
var $vehicleList = $("#vehicles");
$("#vehicles").find(".list-group-item").remove();
$.each(data, function (idx, item) {
$vehicleList.append("<li class=\"list-group-item\">" + item + "</list>");
});
});
});
$("#search").on("click", function () {
var vrnVal = $("#vrn-id").val();
var vrnUrl = "@Url.Action("GetVehicle", "Check")?vrn=" + vrnVal;
$.get(vrnUrl, function (data) {
$("#vehicle").find("li:eq(0)").html("<label>VRN: </label>");
$("#vehicle").find("li:eq(1)").html("<label>Is Shared Vehicle: </label>");
$("#vehicle").find("li:eq(2)").html("<label>Permit: </label>");
$("#vehicle").find("li:eq(3)").html("<label>Permit Valid In Zones: </label>");
$("#vehicle").find("li:eq(4)").html("<label>Last Parked: </label>");
if (!!data) {
$("#vehicle").find("li:eq(0)").html("<label>VRN: </label>" + data["VRN"]);
$("#vehicle").find("li:eq(1)").html("<label>Is Shared Vehicle: </label>" + data["IsSharedVehicle"]);
$("#vehicle").find("li:eq(2)").html("<label>Permit: </label>" + data["PermitNr"]);
$("#vehicle").find("li:eq(3)").html("<label>Permit Valid In Zones: </label>" + data["Zones"]);
$("#vehicle").find("li:eq(4)").html("<label>Last Parked: </label>" + data["pDate"]);
}
});
});
});
</script>
.cs代码:
public JsonResult GetVRNListForZone(int? zoneId)
{
JsonResult json = null;
IList<string> vrnList = null;
if (zoneId == null)
{
vrnList = DbContext.tblSessions
.Where(x => x.CouncilId == Council.Id && !x.ExpireTicket && this.EndOfDayYesterday < x.ParkedOn)
.Select(v => v.tblVehicle.RegistrationNumber).ToList();
}
else
{
vrnList = DbContext.tblSessions
.Where(x => x.ZoneId == zoneId.Value && !x.ExpireTicket && this.EndOfDayYesterday < x.ParkedOn)
.Select(v => v.tblVehicle.RegistrationNumber).ToList();
}
if (vrnList.Any())
{
json = Json(vrnList, JsonRequestBehavior.AllowGet);
}
return json;
}
public JsonResult GetVehicle(string vrn)
{
JsonResult json = null;
var endOfPreviousDay = DateTime.Now.AddDays(-1).EndOfDay();
var session = DbContext.tblSessions.Include(x => x.tblPermit)
.Where(x => x.CouncilId == Council.Id && x.tblVehicle.IsActive && x.tblVehicle.RegistrationNumber == vrn)
//.Where(x => x.CouncilId == Council.Id && x.tblVehicle.IsActive && x.tblVehicle.RegistrationNumber == vrn && endOfPreviousDay < x.ParkedOn)
.OrderByDescending(x => x.ParkedOn)
.FirstOrDefault();
if (session != null)
{
var parkingDetails = new ParkingDetails()
{
SharedVehicleId = session.SharedVehicleId,
PermitId = session.PermitId,
ParkedOn = session.ParkedOn,
PermitSn = session.tblPermit.PermitSn
};
var zones = session.tblPermit.tblZones.Select(x => x.ZoneName).ToList();
string isSharedVehicle = "Yes";
if (parkingDetails.SharedVehicleId == null)
{
isSharedVehicle = "No";
}
json = Json(new
{
VRN = vrn.ToUpper() + ((session.PdParking) ? " (P+D Parking)" : ""),
IsSharedVehicle = isSharedVehicle,
PermitNr = parkingDetails.PermitSn,
Zones = string.Join(", ", zones),
pDate = parkingDetails.ParkedOn.ToString("dd/MMM/yyyy")
}, JsonRequestBehavior.AllowGet);
return json;
}
var vehicle = DbContext.tblUserVehicles.FirstOrDefault(u => u.tblVehicle.RegistrationNumber == vrn && u.tblUser.IsActive);
if (vehicle != null)
{
var userData = Council.tblUsers.FirstOrDefault(u => u.Id == vehicle.IdUser && u.IsActive);
if (userData != null)
{
var permit = DbContext.tblPermits.Find(userData.ActivePermit);
var parkingDetails = new ParkingDetails()
{
SharedVehicleId = 0,
PermitId = permit.Id,
PermitSn = permit.PermitSn
};
var zones = permit.tblZones.Select(x => x.ZoneName).ToList();
json = Json(new
{
VRN = vrn.ToUpper(),
IsSharedVehicle = "No",
PermitNr = parkingDetails.PermitSn,
Zones = string.Join(", ", zones),
pDate = ""
}, JsonRequestBehavior.AllowGet);
return json;
}
}
json = Json(new
{
VRN = "Not Found",
IsSharedVehicle = "No",
PermitNr = "",
Zones = "",
pDate = ""
}, JsonRequestBehavior.AllowGet);
return json;
}
答案 0 :(得分:0)
jQuery默认缓存AJAX响应。您可以使用$.ajax
代替并在必须传入的设置对象中设置cache: false
来禁用它。例如:
$.ajax({
url: vrnUrl,
method: 'GET',
cache: false,
success: function (data) {
var $vehicleList = $("#vehicles");
$("#vehicles").find(".list-group-item").remove();
$.each(data, function (idx, item) {
$vehicleList.append("<li class=\"list-group-item\">" + item + "</list>");
});
})
});
注意:从技术上讲,在jQuery的更高版本中,您仍然可以使用$.get
,但由于您必须传入与$.ajax
相同的设置对象,因此您可以使用$.ajax
并且不担心版本兼容性。
答案 1 :(得分:0)
在搜索我的问题的答案时,我发现完全关闭它的最简单方法是在cshtml页面上使用下面的代码,这使得它总是从表中获取数据。
$ .ajaxSetup({cache:false});
答案 2 :(得分:0)
您可以在操作中禁用缓存:
# test data
Lines <- "December 4, 2016 Sunday, LENGTH: 1070 words, HEADLINE: Korea presents new farm
development model globally, BYLINE: By Yoon Ja-young, BODY: ~~~
December 4, 2016 Sunday, LENGTH: 1070 words, HEADLINE: Korea presents new farm
development model globally, BYLINE: By Yoon Ja-young, BODY: ~~~"
# code
L0 <- readLines(textConnection(Lines))
L <- sub("^", " ", L0)
L <- sub("(.*) LENGTH:", "\nDATE: \\1 LENGTH:", L)
L <- gsub("(\\w+:)", "\n\\1", L)
L <- paste(L, collapse = "\n")
L <- unlist(strsplit(L, "\n"))
dcf <- read.dcf(textConnection(L))
dcf[] <- sub(",$", "", dcf)
dcf[] <- gsub("\n", " ", dcf)
或整个控制器:
> dcf
DATE LENGTH
[1,] "December 4, 2016 Sunday" "1070 words"
[2,] "December 4, 2016 Sunday" "1070 words"
HEADLINE BYLINE
[1,] "Korea presents new farm development model globally" "By Yoon Ja-young"
[2,] "Korea presents new farm development model globally" "By Yoon Ja-young"
BODY
[1,] "~~~"
[2,] "~~~"