3

I have a partial view that I have created called "noteEditor". That is rendered inside of another partial view called "summary". I want the "noteEditor" partial view only rendered to a page once regardless of how many times the "summary" partial view is rendered.

I had hoped to do something like inside the summary partial view:

@{
    var viewDataNotesEditorRegistered = ViewData["notesEditorRegistered"];
    var notesEditorRegistered = (bool)(viewDataNotesEditorRegistered ?? false);
}
@if (!notesEditorRegistered)
{
    <div class="notesdialog" style="display: none;">@Html.Partial("NotesEditor")</div>
    ViewData["notesEditorRegistered"] = true;
}

however, each time this code is called the ViewData["notesEditorRegistered"] it comes back null.

Is there a more "global" (to the entire page and during only that request) scope?

Thanks

1 Answer 1

3

You could use the HttpContext for that purpose instead of ViewData:

@{
    var viewDataNotesEditorRegistered = ViewContext.HttpContext.Items["notesEditorRegistered"];
    var notesEditorRegistered = (bool)(viewDataNotesEditorRegistered ?? false);
}
@if (!notesEditorRegistered)
{
    <div class="notesdialog">@Html.Partial("NotesEditor")</div>
    ViewContext.HttpContext.Items["notesEditorRegistered"] = true;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.