Home Access a viewbag property in reactjs
Reply: 1

Access a viewbag property in reactjs

Maddy Published in 2017-09-14 06:48:07Z

i am developing a web application , where i am using reactjs and MVC c#.

I would want to know if jsx is included in a cshtml , is it possible to access a viewbag property in jsx? i would have a object or id to be passed to jsx which will be rendered in the UI?

Sag1v Reply to 2017-09-14 07:08:38Z

ViewBag is rendered and execute in the server side, react is invoked on the browser (client).
Your main options are:

  1. Global variable - In the razor (.cshtml) you can set a global object window.param = ViewBag.param. and access it from react.
    const x = window.param
  2. Pass a value (primitives only) to the root element of react via a data-attribute and grab it before you call render: example:

        // razor (.cshtml)
            <div id="root" data-param="@ViewBag.param"></div>
       // react
            const root = document.getElementById('root');    
            const param =  root.getAttribute('data-param');
            render(<App myParam={param}/>, root)
You need to login account before you can post.

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

© 2016 Powered by cudou.com design MATCHINFO