Virtual Happy Hour this month, Jun 28, we'll be getting a sneak preview at our soon to launch SaaS CMS!

Try our conversational search powered by Generative AI!

pass string into a shared view using partial call


The way our sites are being set up, all pages use a shared head.cshtml view for all things head. For a couple of page types, they will need some additional info in the page title tag than standard pages get. So, for those pages' layout view, which calls the head using a partial...


I'd like to pass in additional text to the title tag.

I hope that makes sense. Is this possible and if so, how? Thanks!

Apr 04, 2016 15:31

Check out the _Root.cshtml in Alloy project for some examples. For smaller changes you can just pass the partial view a model like

 Html.RenderPartial("Header", Model);

for Alloy the use a viewmodel that contains both the current page and some general layout info. 

@model IPageViewModel<SitePageData>

If you just have a few presentation details like formatting title differently that you want to modify, you can probably implement that in your partial view depending on what type the current page is (that is passed in through the model above) and page name etc. If you need more control like fetching from external datasource or similar I would probably extend the viewmodel with a separate field and fill that in the controller and then pass it in. 

Alloy uses a layout view model like

 public class LayoutModel
        public SiteLogotypeBlock Logotype { get; set; }
        public IHtmlString LogotypeLinkUrl { get; set; }
        public bool HideHeader { get; set; }
        public bool HideFooter { get; set; }
        public LinkItemCollection ProductPages { get; set; }
        public LinkItemCollection CompanyInformationPages { get; set; }
        public LinkItemCollection NewsPages { get; set; }
        public LinkItemCollection CustomerZonePages { get; set; }
        public bool LoggedIn { get; set; }
        public MvcHtmlString LoginUrl { get; set; }
        public MvcHtmlString LogOutUrl { get; set; }
        public MvcHtmlString SearchActionUrl { get; set; }


that you can extend to your hearts content

Apr 04, 2016 16:22

Hi Nathan,

If you just want to pass string (or any other object) to the view using Html.Partial helper, you can do it like this:


    var additionalInfo = ViewBag.additionalInfo as string;

@* Rest of the code *@

@if (!string.IsNullOrEmpty(additionalInfo))
    <p>Additional info: @additionalInfo</p>

Index.cshtml (or any other view)

@Html.Partial("Head", Model, new ViewDataDictionary { { "additionalInfo", "Lorem ipsum dolor"} })

Hope this helps!

Apr 04, 2016 21:28

I perfer Daniel's suggestion, to populate the shared data in the layout view.

Somehow, if that's too much change in your solution, you can use the ViewData and pass into  the Partial helper.

Apr 05, 2016 3:06
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.