简体   繁体   English

访问reactjs中的viewbag属性

[英]Access a viewbag property in reactjs

i am developing a web application , where i am using reactjs and MVC c#. 我正在开发一个Web应用程序,我正在使用reactjs和MVC c#。

I would want to know if jsx is included in a cshtml , is it possible to access a viewbag property in jsx? 我想知道jsx是否包含在cshtml中,是否可以访问jsx中的viewbag属性? i would have a object or id to be passed to jsx which will be rendered in the UI? 我会有一个对象或id传递给jsx,它将在UI中呈现?

ViewBag is rendered and execute in the server side, react is invoked on the browser (client). ViewBag在服务器端呈现并执行,在浏览器(客户端)上调用react。
Your main options are: 您的主要选择是:

  1. Global variable - In the razor ( .cshtml ) you can set a global object window.param = ViewBag.param . 全局变量 - 在剃刀( .cshtml )中,您可以设置全局对象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: 通过data-attribute将值(仅基元)传递给react的根元素,并在调用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) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM