admin管理员组

文章数量:1422077

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?

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?

Share Improve this question edited Apr 28, 2019 at 6:37 Sagiv b.g 31k10 gold badges72 silver badges104 bronze badges asked Sep 14, 2017 at 6:48 MaddyMaddy 1051 silver badge11 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

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)
    

本文标签: javascriptAccess a viewbag property in reactjsStack Overflow