1

I want to create a blog with sub-blogs. Each sub-blog would have it's own "theme" variable in the model which would alter the theme. I try to pass the theme variable to the _ViewStart.vbhtml page like so:

@Code

@((@ViewBag.Theme.length > 0) ? Layout = @ViewBag.Theme : Layout = "~/Views/Shared/_Layout.vbhtml")
     End Code

However, I get an error:

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: BC30201: Expression expected.

Source Error:

Line 5: *@ Line 6: Line 7: @((@ViewBag.Theme.length > 0) ? Layout = @ViewBag.Theme : Layout = "~/Views/Shared/_Layout.vbhtml") Line 8: Line 9: End Code

Source File: C:\Users\darchual\documents\visual studio 2010\Projects\GemcoBlog\GemcoBlog\Views_ViewStart.vbhtml Line: 7

How can I change templates, is there a better way?

user1477388
  • 20,790
  • 32
  • 144
  • 264

1 Answers1

0

try it as

@{
    Layout = (ViewBag.Theme.length > 0) ? ViewBag.Theme : "~/Views/Shared/_Layout.vbhtml"
}

Just as a warning though, if theme is null then length will fail. Could be better to do

@{
    Layout = !String.IsNullOrWhiteSpace(ViewBag.Theme) ? ViewBag.Theme : "~/Views/Shared/_Layout.vbhtml"
}

Edit: Just for the record, yes setting the different view in the answer you linked (stackoverflow.com/questions/5059323) is better and correct way to do it, but just to note if you wanted to stick to your approach this works for me:

@{
    Layout = !String.IsNullOrWhiteSpace((String)ViewContext.ViewData["Theme"]) ? (String)ViewContext.ViewData["Theme"] : "~/Views/Shared/_Layout.cshtml";
}
Community
  • 1
  • 1
Manatherin
  • 4,169
  • 5
  • 36
  • 52
  • I tried it but it gives me a syntax error on "@{" it says, "Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately." In my VS2010, it says, "Error 10 'ViewBag' is not declared. It may be inaccessible due to its protection level." – user1477388 Jul 26 '12 at 15:27
  • @user1477388 Ah sorry, just noticed your trying to do it from _ViewStart which can't access ViewBag. You could use PageData (as shown in http://stackoverflow.com/questions/4834045/how-do-i-set-viewbag-properties-on-viewstart-cshtml) or declare it at the top of your pages (included where ViewBag.Title is normally declared). – Manatherin Jul 26 '12 at 15:30
  • @user1477388 Another alternative shown in http://stackoverflow.com/questions/6578029/why-cant-viewstart-cshtml-access-the-viewbag-object. You may be able to access it through `ViewContext.ViewData["Theme"]` – Manatherin Jul 26 '12 at 15:34
  • I am using this now, but it gives an error again:@Code Layout = (@ViewContext.Controller.ViewBag.Theme) ? ViewBag.Theme : "~/Views/Shared/_Layout.vbhtml" End Code – user1477388 Jul 26 '12 at 15:37
  • Same as before: Compilation Error Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. Compiler Error Message: BC30201: Expression expected. Source Error: Line 3: Line 4: Line 5: Layout = (@ViewContext.Controller.ViewBag.Theme) ? @ViewContext.Controller.ViewBag.Theme : "~/Views/Shared/_Layout.vbhtml" Line 6: Line 7: – user1477388 Jul 26 '12 at 15:40
  • It actually seems as though this is the solution http://stackoverflow.com/questions/5059323/asp-mvc-3-use-different-layouts-in-different-views – user1477388 Jul 26 '12 at 15:49