'How to edit navbar from another page using Layout?
I've a navbar in my _Layout.cshtml, it includes home, register, login. When the user is registered and only at this time I want navbar to contain also a new element as a little image, which will redirect to some page. How can I add smth to my navbar from the page which's using this _Layout.cshtml?
Solution 1:[1]
You can put a flag in your navbar in _Layout.cshtml page.
- Get data from the database to know if your use is registered or not? If yes then set the flag to TRUE in your ViewBag.
Code in your controller:
ViewBag.IsRegistered= true;
- And pass this model in your cshtml page
Here is the sample:
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li>
<a href="@Url.Action("Index", "Home")">
<span class="glyphicon glyphicon-home"></span>
Home
</a>
</li>
<li>
<a href="@Url.Action("Register", "Register")">
<span class="glyphicon glyphicon-search"></span>
Register
</a>
</li>
<li>
<a href="@Url.Action("Login", "Login")">
Login
</a>
</li>
@{
if(@ViewBag.IsRegistered){
<li>
<a href="@Url.Action("Search", "Home")">
<img src="path to image" alt="test">
</a>
</li>
}
}
</ul>
</div>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | heyharshil |
