robin
robin

Reputation: 497

Read object values in Razor

I have a ViewData that contains a string like that.

{ Param1 = "1", Param2 = "2", Param3 = "3" }

I'm setting it to an object razor variable, but I can't read this values like @myVar.Param1.

@{
    object myParameters = ViewData["parameters"];
}

I know how to do that in JS, but not in Razor. What is the best way to do this?

Regards

Upvotes: 0

Views: 345

Answers (2)

Andrei
Andrei

Reputation: 56716

Since you are using object of anonymous type defined elsewhere (in controller), you cannot have strong typed access to the properties. However you should be able to use Eval:

ViewData.Eval("parameters.Param1")

Although I encourage you to consider defining a class for this model.

Upvotes: 1

Jeff Sulit
Jeff Sulit

Reputation: 26

myParameters should be dynamic:

dynamic myParameters = ViewData["parameters"];

Upvotes: 1

Related Questions