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?

1 Answer 1

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";
}
6
  • 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." Jul 26, 2012 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 stackoverflow.com/questions/4834045/…) or declare it at the top of your pages (included where ViewBag.Title is normally declared).
    – Manatherin
    Jul 26, 2012 at 15:30
  • @user1477388 Another alternative shown in stackoverflow.com/questions/6578029/…. You may be able to access it through ViewContext.ViewData["Theme"]
    – Manatherin
    Jul 26, 2012 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 Jul 26, 2012 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: Jul 26, 2012 at 15:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.