ASP.Net MVC 在母版页中显示会话值

发布于 2024-10-09 19:29:59 字数 156 浏览 0 评论 0原文

当我在母版页中显示会话值 (<%: Session["companyname"].ToString() %>) 时,页面上会显示以下信息 { CompanyName =测试公司}。我怎样才能得到这个值?

感谢您的帮助!

When I display a session value in a master page (<%: Session["companyname"].ToString() %>) the following information is displayed on the page { CompanyName = TestCompany}. How do I get just the value?

Thanks for your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我是有多爱你 2024-10-16 19:29:59

如果您可以显示该值在会话中存储位置的代码,我更有可能提供帮助。不过,我建议您可能需要重新考虑直接在视图中使用会话中的值。在我看来,最好拥有一个所有视图模型派生自的基本视图模型,该模型上具有 CompanyName 属性以及母版页所需的任何其他常见属性。然后,您的母版页可以强类型化为基本视图模型,并且您可以使用模型中的值。我在几个项目中使用了这种模式并取得了良好的成功。将其与基本控制器结合使用,其中为 OnActionExecuted() 中的视图结果填充公共属性,它可以非常有效地减少代码重复和魔术字符串的使用。在你看来。

型号:

public abstract class CommonViewModel
{
    public string CompanyName { get; set; }
    public string UserDisplayName { get; set; }
    ...
}

控制器:

public abstract class BaseController
{
    public override void OnActionExecuted( ActionExecutedContext filterContext )
    {
        if (filterContext.Result is ViewResult)
        {
            var model = filterContext.ViewData.Model as CommonViewModel;
            if (model != null)
            {
                 model.CompanyName = Session["CompanyName"] as string;
                 model.UserDisplayName = Session["UserDisplayName"] as string;
            }
        }
    }
}

母版页:

<%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage<Foo.CommonViewModel>" %>
<!-- ... -->
<div>
   <%: Model.CompanyName %>
</div>
<!-- ... -->
<div>
   <%: Model.UserDisplayName %>
</div>

If you can show the code where the value is stored in the session, it's more likely that I could help. I would suggest, though, that you might want to reconsider using the value from the session directly in your view. It would be better, in my opinion, to have a base view model that all of your view models derive from that has the CompanyName property, and any other common properties required by your master page, on it. Your master page, then, could be strongly-typed to the base view model and you could use the values from the model. I've used this pattern with good success on a couple of projects. Couple it with a base controller where the common properties are populated for view results in OnActionExecuted(), it can be very effective in both reducing code duplication and the use of magic strings in your views.

Model:

public abstract class CommonViewModel
{
    public string CompanyName { get; set; }
    public string UserDisplayName { get; set; }
    ...
}

Controller:

public abstract class BaseController
{
    public override void OnActionExecuted( ActionExecutedContext filterContext )
    {
        if (filterContext.Result is ViewResult)
        {
            var model = filterContext.ViewData.Model as CommonViewModel;
            if (model != null)
            {
                 model.CompanyName = Session["CompanyName"] as string;
                 model.UserDisplayName = Session["UserDisplayName"] as string;
            }
        }
    }
}

Master Page:

<%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage<Foo.CommonViewModel>" %>
<!-- ... -->
<div>
   <%: Model.CompanyName %>
</div>
<!-- ... -->
<div>
   <%: Model.UserDisplayName %>
</div>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文