如何在Mvc C#中从控制器更改主要布局内容? [英] How Do I Change Main Layout Content From Controller In Mvc C#?

查看:135
本文介绍了如何在Mvc C#中从控制器更改主要布局内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从MVC c#中的控制器更改主要布局内容?



我有一个布局,主视图和用户视图。

该布局包含一个菜单栏和页脚。

我将此布局用于主视图和用户视图。此布局包含带登录和注册选项的菜单栏。

当用户登录时,我将显示用户页面视图(但具有相同的布局)。我需要隐藏登录和注册选项,而不是我想要显示登录的用户名。



有可能吗?如果是的话怎么样?

实际上我的主要布局在顶部栏上有3个菜单链接。接下来登录并注册。我想在用户登录后隐藏它。

我是否需要为用户视图页面创建不同的布局?

How do i change main layout content from controller in MVC c#?

I have one layout,home view and user view.
That layout contains one menu bar and footer.
Am using this layout for home view and User view. This layout contains menu bar with log in and sign up options.
When the user logged in i will show user page view (but with same layout). I need to hide log in and sign up options instead of that i want to show logged in user name.

Is it possible? If yes how?
Actually my main layout has 3 menu links on top bar.Next to that login and sign up are there. I want to hide this once user get logged in.
Do i need to create different layout for user view page?

推荐答案

a。在布局中声明部分为

@RenderSection(Account_Control,必需:false)



b。 Account_Control是局部视图。其中包含Login和SignUp UI元素。



c。在View中定义Account_Control部分,用于经过身份验证的用户。



d。显然我们没有为查看定义Account_Control部分,这是匿名用户。





这是一种方式,你在哪里可以达到你需要的相同要求。
a. Declare section in layout as
@RenderSection("Account_Control", required: false)

b. Account_Control is partial view. which contain Login and SignUp UI elements.

c. Define Account_Control Section in View which is for Authenticated User.

d. Obviously we don't have to Define Account_Control Section for view , which is for Anonymous user.


this is one way, where you can achieve the same requirement as you needed.


这篇关于如何在Mvc C#中从控制器更改主要布局内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆