0

I have a view with some javascript in it. The javascript has a callback function that is called, when the data i need is ready (from indexeddb).

This data, i want to pass to a partial view - How do i do that? I cannot seem to use ViewData, as that expires as soon as the page renders (thus, before the callback is received). Any ideas? Code is here:

<div>
    <script type="text/javascript">
        var somecallback = myFunctionDefinedSomewhere();
        somecallback.onsuccess = function(evt) {
            console.log("Success!");

            var iReallyNeedThisVariable = evt.id;
            @ViewData["iReallyNeedThisVariable"] = iReallyNeedThisVariable;
        }
    </script>

    @for (int i = 0; i < Model.MyCollection.Count; i++)
    {
        if (i == 0)
        {
            @Html.Partial("_MyPartialView", Model.MyCollection.ElementAt(i), new ViewDataDictionary { { "Stuff", true }, { "iReallyNeedThisVariable", @ViewData["iReallyNeedThisVariable"] } });
        }
        else
        {
            @Html.Partial("_MyPartialView", Model.MyCollection.ElementAt(i), new ViewDataDictionary { { "Stuff", false }, { "iReallyNeedThisVariable", @ViewData["iReallyNeedThisVariable"] } });
        }
    }
</div>
6
  • 4
    you are mixing Server side code with JavaScript. You cannot set ViewBag from JavaScript. Instead you need to make a AJAX POST/GET call to Controller Action, then in response either get PartialView or JSON Data and use it in your main view. Commented Jun 20, 2015 at 10:25
  • I'm trying to avoid any serverside code - im using serviceworkers to make the site work while offline Commented Jun 20, 2015 at 10:27
  • 1
    unfortunately you cannot do this Commented Jun 20, 2015 at 10:33
  • I am not sure what you mean by avoiding Server side code and work while offline. Can you describe your question in detailed? Because even if you want to render partialview dynamically, there should be server hit. Commented Jun 20, 2015 at 10:34
  • ServiceWorkers can cache any "normal" http requests, and serve them to the browser when the browser is offline. It does not, AFAIK, support AJAX in the same manner. Besides, this is entirely client-side. I open the client-side indexeddb, and render the page client-side. I just want to delay the rendering of the partial views, till i have an open connection to indexeddb Commented Jun 20, 2015 at 10:36

1 Answer 1

2

you are mixing Server side code with JavaScript. You cannot set ViewBag from JavaScript.

Instead you need to make a AJAX POST/GET call to Controller Action (or to the Service Workers), then in response either get PartialView or JSON Data and use it in your main view.

UPDATE: OP confirmed that AJAX call is supported by Service Worker's Fetch event.

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.