Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at

  • Paul 18 posts 159 karma points
    Nov 17, 2017 @ 11:46

    Passing boolean data into a partial view and using it to conditionally display a class

    Hi all, many thanks in advance for your help.

    In my template view:

    @Html.Partial("HeaderSearchForm", new ViewDataDictionary {{"isExpandable", true}})

    In my HeaderSearchForm.cshtml

        var isExpandableValue = (bool)ViewData["isExpandable"];
        var isExpandable = (isExpandableValue) ? "-form expandable-search" : "";

    I'm getting a nullreference error on line 5

    Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
    Source Error: 
    Line 3:  @{
    Line 4:      var thing = (bool)ViewData["isExpandable"];
    Line 5:      var isExpandable = (thing) ? "-form expandable-search" : "";
    Line 6:  }
    Line 7:  
    Source File: c:\inetpub\wwwroot\mhshomes\Views\Partials\HeaderSearchForm.cshtml    Line: 5 

    Here's the html for reference:

    <form class="[email protected](isExpandable)" role="search" method="POST" action="/Handlers/SiteSearch.ashx">
        <input type="text" value=" " class="search-input" name="search-box" placeholder="Search...">
        <input type="hidden" id="current-node-id" name="current-node-id" value="@Model.Content.Id" />
        <button type="submit" class="search-submit"><i class="fa fa-search"></i></button>

    Currently migrating the platform to MVC views, learning Umbraco and seeing the end of the tunnel. Exciting times.

  • Dan Diplo 1505 posts 5905 karma points MVP 4x c-trib
    Nov 17, 2017 @ 13:20
    Dan Diplo

    What does your HeaderSearchForm partial inherit from? It needs to inherit from UmbracoTemplatePage for ViewData to be available, I believe.

  • Paul 18 posts 159 karma points
    Nov 17, 2017 @ 16:23

    Thanks for your reply Dan,

    I do have this at the top of the file

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
  • Dan Diplo 1505 posts 5905 karma points MVP 4x c-trib
    Nov 17, 2017 @ 16:39
    Dan Diplo

    Strange, then it should work, assuming you haven't made any typos. I can't see any reason for it not working!

    But you could try an alternative approach of passing a model to your partial view that has one boolean field. It's a little neater, but requires a bit more code. So you'd need to create a class, similar to:

    public class SearchConfigModel
        public bool IsExpandable { get; set; }

    Then pass it to your partial:

    @Html.Partial("HeaderSearchForm", new SearchConfigModel() { IsExpandable = true } })

    Then make your partial inherit from UmbracoViewPage:

    @inherits UmbracoViewPage<SearchConfig>

    You can then access Model.IsExpendable in your partial view.

  • Frans Lammers 34 posts 297 karma points c-trib
    Jun 19, 2019 @ 12:28
    Frans Lammers

    A belated answer, but I got the same problem and found the answer. I thought I'd share it here.

    The default call to a partial:


    passes the current model to the partial. You used:

    @Html.Partial("HeaderSearchForm", new ViewDataDictionary {{"isExpandable", true}})

    This however does not pass the model, only the ViewDataDictionary. So to pass them both you need to use a different overload of Html.Partial:

    @Html.Partial("HeaderSearchForm", Model, new ViewDataDictionary {{"isExpandable", true}})
Please Sign in or register to post replies

Write your reply to: