如何在除布局之外的mvc中添加母版页/如何将数据传递给布局 [英] how to add masterpage in mvc other than layout/ how to pass data to a layout

查看:80
本文介绍了如何在除布局之外的mvc中添加母版页/如何将数据传递给布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将母版页添加到我的mvc应用程序中,但它不是布局我需要编写materpage.cs代码,所以要添加它以及我在添加视图时如何选择它。



或者我需要在mvc中使用过滤器。如果是这样我怎么能用这个。我是mvc的初学者



或者如何将数据传递给布局

I wanna add master page to my mvc application but its not layout i need to write materpage.cs code so were to add it and how i select this when im adding a view.

Or i need to use filters in mvc. If so how can i use this .Im beginner for mvc

Or how to pass data to a layout

推荐答案

模型视图控制器的概念不支持文件后面的代码。所以,如果你想编写不直接或不应该的masterpage.cs。



如果你想过滤,那么你必须清楚地提到你想要使用什么类型的过滤器。像动作过滤器和其他类似的过滤类型很少。



然后你的上一个问题,如果你想改变_layout的控制值。然后将其作为局部视图。出于特定原因或在操作中,如果要更改布局控件的值,请再次调用局部视图。
The concept of Model View Controller does not support code behind file. So, if you want to write masterpage.cs that is not directly possible or should not be.

If you want to filter in then you have to mention clearly, what type of filter you want to use. there are few types of filtering like action filter and others.

Then your last question, if you want to change a value of _layout's control. Then make it as a partial view. For a specific reason or in an action if you want to change the value of layout control then call the partial view again.


对于此场景,过滤将更加昂贵。所以使用局部视图。例如,部分视图名称为LoggedInInfo.cshtml。



_Layout页面你必须每次使用@ Html.Action(loggedInAction,Account)等动作来调用此视图。在此操作中,您必须检查您的表单是否经过身份验证。如果是,则使用viewbag或viewdata将用户名传递给局部视图。 Ohterwise什么也不做。



这里应该注意,你必须在当前环境中保留用户的Idenetity,这可以从你的应用程序的任何地方访问。



所以,我的建议是使用表单身份验证并在当前上下文中保持用户的idenetity,并将登录信息作为部分视图。
for this scenario, filtering will be more expensive. So use a partial view. For example, the partial view name is "LoggedInInfo.cshtml".

_Layout page you have to call this view using an action, like @Html.Action("loggedInAction","Account") every time. Inside this action you have to check that your form is authenticated or not. if yes then pass the user name to the partial view using viewbag or viewdata. Ohterwise do nothing.

It should be noted here, you have to keep the Idenetity of user in Current Context which is accessible from any where your application.

So, My suggestion is to use Form authentication and keep the user idenetity in current context and make the logged in info as a partial view.


这篇关于如何在除布局之外的mvc中添加母版页/如何将数据传递给布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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