2

I need to pass List to _LayOut.Cshml in ASP.NET MVC. Let us say, I want to show city dropdown next to Login. It will be there for all views.

I know few options like creating a base controller class , Action filter

But can not getting it work. Any help ?

debugmode
  • 936
  • 7
  • 13
  • Can you show us your piece of code ? – Tushar Gupta Jan 04 '16 at 06:51
  • I don't have a code to show. let us say I will set viewbag.city = List. I want this viewbag to be present fro _layout.cshtml. Also I dont want to set it in all controllers. – debugmode Jan 04 '16 at 06:52
  • Check these questions: http://stackoverflow.com/questions/26224790/using-viewbag-in-layout-page http://stackoverflow.com/questions/20204424/asp-net-mvc5-access-viewbag-from-layout – adiga Jan 04 '16 at 07:09

1 Answers1

0

You should use a ViewComponent to perform this functionality. The view component can load the list from your data source and then bind to a strongly typed model in the view component's Default.cshtml.

Then in your _Layout.cshtml file you simply call the ViewComponent like such:

@await Component.InvokeAsync("CityList", new { state= "MA" })

You can read more about View Components here: http://www.davepaquette.com/archive/2016/01/02/goodbye-child-actions-hello-view-components.aspx

CodingSamurai
  • 101
  • 1
  • 7