[英]MVC controller being loaded multiple times
My goal: 我的目标:
The user session will keep track of guid's stored in Session.Add(guid.tostring()). 用户会话将跟踪存储在Session.Add(guid.tostring())中的guid。
When a partial is refreshed inside a div, the controller will check for existing guids. 当在div中刷新局部时,控制器将检查现有的向导。 This will let me track what notifications need displayed, what are already being displayed to avoid duplicates ..etc. 这将让我跟踪需要显示哪些通知,已经显示了什么以避免重复..etc。
The Problem: 问题:
When a notification should be displayed again, it isn't even though I see it in the model being passed to the view 当应该再次显示通知时,即使我在传递给视图的模型中看到通知也没有
What I think is the cause 我认为是原因
For some reason when I debug the controller as the very start of the method to load the partial, it's being loaded many times which I believe is why when a notification should be displayed, it isn't. 由于某种原因,当我调试控制器作为加载部分方法的开始时,它已被加载了很多次,我相信这就是为什么在显示通知时没有显示的原因。
Main Index View that refreshes the partial. 刷新部分的主索引视图。 #overview. #overview。 Also the interval is every 15 seconds. 而且间隔是每15秒一次。
function intervalTrigger() {
$('#overview').load('/Home/Overview');
};
<div id="overview">
@{Html.RenderPartial("Overview", "Home");}
</div>
Code inside Overview partial that displays the alerts 显示警报的概述部分中的代码
@for (int i = 0; i < Model.DisplayAlerts.Count(); i++)
{
@:$(function () {
@:$.Notify({
@:keepOpen: true,
@:caption: '@Model.DisplayAlerts[i].SectionName',
@:content: '@Model.DisplayAlerts[i].ParamDescription <br/> @Model.DisplayAlerts[i].DetailDatetime.ToString("MM/dd/yyyy hh:mm:ss tt ")',
@:type: 'alert'
@:});
@:});
}
Code that is populating the model being handed to the view 正在填充传递给视图的模型的代码
OverviewModel model = new InformationController().GetOverviewModel();
model.DisplayAlerts = new List<BasicSectionDetailModel>();
List<BasicSectionDetailModel> details = new Collector.InfoCollector().GetSectionDetailsNotOKState();
List<Guid> sessionKeys = new List<Guid>();
for (int i = 0; i < Session.Contents.Count; i++)
{
Guid guid;
if (Guid.TryParse(Session.Keys[i].ToString(), out guid))
{
sessionKeys.Add(guid);
}
}
List<BasicSectionDetailModel> addAlert = details.Where(x => !sessionKeys.Any(x2 => x2 == x.ID)).ToList();
foreach (BasicSectionDetailModel alert in addAlert)
{
Session.Add(alert.ID.ToString(), null);
}
List<Guid> removeAlert = sessionKeys.Where(x => !details.Any(x2 => x2.ID == x)).ToList();
foreach (Guid remove in removeAlert)
{
Session.Remove(remove.ToString());
}
model.DisplayAlerts = addAlert;
return model;
I found the issue 我发现了问题
model.DisplayAlerts = addAlert;
addAlert was being destroyed. addAlert被销毁了。 I had to copy the contents into the model and not assign one var to another. 我必须将内容复制到模型中,而不是将一个变量分配给另一个。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.