Kumar
Kumar

Reputation: 11349

How to mix server side code in razor

A trivial question but the solution escapes me at the moment

We have a CMS where the Layout and CSS ( names only, no extension ) are retrieved from db !

the .cshtml has this code which does NOT compile cleanly

....
 <link href="~/Content/themes/@ViewBag.dbConfig.Theme/style/@ViewBag.dbConfig.CssName.css" rel="stylesheet">    

where the variable is

  ViewBag.dbConfig.CssName

without the .css extension

Is there a way to make this work without changing the config ?

Upvotes: 0

Views: 157

Answers (2)

JC Lizard
JC Lizard

Reputation: 1056

use parantesese and trim:

<link href="~/Content/themes/@(ViewBag.dbConfig.Theme.trim())/style/@(ViewBag.dbConfig.CssName.trim()+".css")" rel="stylesheet"> 

Upvotes: 0

Nick Albrecht
Nick Albrecht

Reputation: 16928

First I would wrap your variable usages... See if that yields something more in line with what you expected.

<link href="~/Content/themes/@(ViewBag.dbConfig.Theme)/style/@(ViewBag.dbConfig.CssName).css" rel="stylesheet">

Upvotes: 1

Related Questions