Qar
Qar

Reputation: 1804

React with .net c# razor templates

I have some razor templates with some model data that are being rendering as for example form labels.

If I add this form as a react component - how would you render the model data?

One solution would be to make the react code inline in the document and just parse it in directly. Another solution would be to make a global js array inline in the razor document and then set a "data" state using this array. But both of these methods doesn't feel right...

I'm using browserify and have my react components as seperate .js files.

What do you guys to do render serverside data (which can't be fetched via ajax requests) into your components?

Upvotes: 2

Views: 2060

Answers (1)

Mike M
Mike M

Reputation: 488

If I understand your question correctly you want to pass in the model data from your view to React.

In your view you just pass the model to react like so

@Html.React("*ReactFileName*", Model)

Then in your Jsx file you just need to receive the model using

constructor(props){
   super(props);
   this.state = {
        *stateVariable*: props
   };
 }

Then in the bottom of the Jsx

module.exports = *ModelName*;

Upvotes: 2

Related Questions