[英]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: 您的主要选择是:
.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
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.