获取数据到使用MVC的局部视图或布局 [英] Getting data into a partial view or layout using MVC
本文介绍了获取数据到使用MVC的局部视图或布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要显示的信息登录在布局和/或部分景色用户(用户名,公司名称,通知的数量等)。这些都将是共同的每一页上。有没有得到这个数据给他们的伎俩,或者是延长每个模型在他们这些信息的情况?
I want to display information about the logged in user (username, company name, number of notifications, etc) in the layout and/or partial views. These will be common on every page. Is there a trick for getting this data to them, or is it a case of extending each model to have this information in them?
推荐答案
我建议你可以去为孩子的行动,并从布局调用它,通过这种方式就可以避免开展的所有视图模型的信息。
I would suggest you can go for a child action and invoke it from the layout, By this way you can avoid carry out the information in all the view models.
例
儿童行动
public class UserController
{
[ChildActionOnly]
public PartialViewResult UserInfo()
{
var userInfo = .. get the user information from session or db
return PartialView(userInfo);
}
}
管窥
@model UserInfoModel
@Html.DisplayFor(m => m.UserName)
@Html.DisplayFor(m => m.CompanyName)
...
布局视图
<header>
@Html.Action("UserInfo", "User")
</header>
这篇关于获取数据到使用MVC的局部视图或布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文