javascript - Access a viewbag property in reactjs - Stack Overflow

i am developing a web application , where i am using reactjs and MVC c#. I would want to know if jsx is

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)
    

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745328682a4622775.html

相关推荐

  • javascript - Access a viewbag property in reactjs - Stack Overflow

    i am developing a web application , where i am using reactjs and MVC c#. I would want to know if jsx is

    4小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信