简体   繁体   中英

MVC controller being loaded multiple times

My goal:

The user session will keep track of guid's stored in Session.Add(guid.tostring()).

When a partial is refreshed inside a div, the controller will check for existing guids. This will let me track what notifications need displayed, what are already being displayed to avoid duplicates ..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. Also the interval is every 15 seconds.

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. I had to copy the contents into the model and not assign one var to another.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM