Andrew
Andrew

Reputation: 5093

Razor: Get style value from elsewhere on the page

I'm trying to hack my way through some ASP.NET pages put together with Razor, having never seen Razor used before today, more or less, and I was wondering if it were possible to grab a CSS style value and use it in Razor code, like this:

@foreach (var item in someList.Where(i => someHTMLElement.display == block
                               ? i.property == "value"
                               : i.property == "othervalue"))
{
     ... display filtered list
}

It's that Where bit I'd like to populate with something useful. Any suggestions?

Upvotes: 1

Views: 1030

Answers (2)

Brian Mains
Brian Mains

Reputation: 50728

You'd have to leverage some server-side feature for this. Razor views rendered hierarchically , so the value to display must be defined either in the controller (if using MVC), or above where you intend to implement it. But it's up to you to make the determination on the server, or use JavaScript to replicate this logic on the client...

Upvotes: 1

Darin Dimitrov
Darin Dimitrov

Reputation: 1039438

Razor runs on the server much earlier a DOM tree is constructed by the client browser. This means that you do not have access to other DOM elements with Razor. The best way to achieve that would be to simply adapt your view model and include the necessary properties to it and have the controller populate them. So in the view all you have to do is a simple test of some property.

Upvotes: 3

Related Questions