Home Passing Viewbag Data from Controller to Partial View Returns Null
Reply: 0

Passing Viewbag Data from Controller to Partial View Returns Null

user2374
1#
user2374 Published in June 20, 2018, 1:23 pm

I am having issues passing Viewbag data from my controller action to my partial view. It keeps on returning null and I'm struggling to understand why. I have gone through about every article on using Viewbag and my code seems to be correct.

My Controller Action:

[HttpGet]
    public ActionResult _FileList(int id)
    {
        ClaimViewModel claim = new ClaimViewModel();
        claim.document = _documentService.GetDocument(session.customerId, id);

        string filePath = _fileService.ViewFile("azureContainer", claim.document.DocumentPath);

        ViewBag.file = filePath;

        return PartialView("_FileList");
    }

My Main View:

@Html.Partial("_FileList", Model.Documents)

My Partial View (_FileList.cshtml):

<div class="list-group">
        @foreach (var file in Model)
        {
            <button type="button" id="docsButton" class="btn btn-default" data-toggle="modal" data-target="#docsModal" 
                    onclick="_viewer.OpenUrl('@ViewContext.ViewBag.file'); return false;">
                @file.Name           
            </button>
        }
    </div>

The ViewBag is supposed to store a string to a URL to a specific file, but in the partial view it returns null. I've tried different workarounds, such as adding in new ViewDataDictionary(ViewData) { { "file", ViewBag.file } } to my call to the partial view in my main view, but I haven't been able to get anything to work. I know my filePath in my controller is not null. I've tested that to make sure. So, I'm not entirely sure what is going wrong here and was wondering if someone could help me out. I've already checked out the different threads on this site about the issue, but none of the solutions have been helpful. I need to have Model.Documents in my @Html.Partial because I need to reference a completely separate model as well. Does anyone understand what might be going on here? I feel like I'm misunderstanding something either within my controller or my partial view.

Note: I am using Atalasoft's Web Document Viewer, which is where the _viewer.OpenUrl() comes from.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.390915 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO