Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET C# MVC How to get a property of controller from the View? [closed]

Tags:

c#

asp.net-mvc

How to get a property of controller from the View "_Layout.cshtml"?

I have the property "DisplayName" (string) and i need get this property in view.

like image 742
Renato Leite Avatar asked Sep 13 '13 17:09

Renato Leite


1 Answers

You can't access properties of a controller from view or layout because the controller that started particular view is gone by the time view is rendered.

Instead of trying to get access to controller you should pass information via model or ViewBag:

In controller:

ViewBag.DisplayName = this.DisplayName;

In views:

@ViewBag.DisplayName

Note that there could be multiple controllers involved into rendering of the page.

like image 127
Alexei Levenkov Avatar answered Sep 27 '22 20:09

Alexei Levenkov