仅在视图中调用一次控制器

问题描述 投票:0回答:1

我的 ASP.NET Core MVC 项目中有一个视图,它返回顶部栏的数据。

这里是视图

   @using Abp.MultiTenancy
@using Abp.Timing
@using DispatcherWeb.Features
@using DispatcherWeb.Web.Areas.App.Models.Layout
@model HeaderViewModel
@{
    Layout = null;
    var isChatEnabled = (AbpSession.MultiTenancySide == MultiTenancySides.Host || IsFeatureEnabled(AppFeatures.ChatFeature));
}

<div id="m_header_topbar" class="m-topbar  m-stack m-stack--ver m-stack--general">
    <nav class="m-stack__item m-topbar__nav-wrapper"
         aria-label="@L("TopMenu")">
        <ul class="m-topbar__nav m-nav m-nav--inline"
            aria-label="@L("TopMenu")"
            role="menubar">
            <li class="m-nav__item m-topbar__user-profile m-topbar__user-profile--img  m-dropdown m-dropdown--medium m-dropdown--arrow m-dropdown--header-bg-fill m-dropdown--align-right m-dropdown--mobile-full-width m-dropdown--skin-light"
                m-dropdown-toggle="click"
                id="UserProfileMenuItem"
            >
                <a href="#" class="m-nav__link m-dropdown__toggle"
                   role="menuitem"
                   aria-haspopup="true"
                   aria-expanded="false"
                   tabindex="0"
                   id="UserProfileMenuItemLink">
                    <span class="m-topbar__username">
                        @if (Model.IsImpersonatedLogin)
                        {
                            <i class="fa fa-reply m--font-danger"></i>
                        }
                        @Html.Raw(Model.GetShownLoginName())
                    </span>
                    <span class="m-topbar__userpic">
                        <img alt="" class="header-profile-picture m--img-rounded m--marginless m--img-centered" src="@Url.Action("GetProfilePicture", "Profile", new {area = string.Empty})[email protected]()" />
                    </span>
                </a>
                <div class="m-dropdown__wrapper" id="UserProfileDropdown">
                    <span class="m-dropdown__arrow m-dropdown__arrow--right m-dropdown__arrow--adjust"></span>
                    <div class="m-dropdown__inner">
                        <div class="m-dropdown__header m--align-center profile-image-fallback-container">
                            <div class="m-card-user m-card-user--skin-dark">
                                <div class="m-card-user__pic">
                                    <img alt="" class="header-profile-picture m--img-rounded m--marginless m--img-centered" src="@Url.Action("GetProfilePicture", "Profile", new {area = string.Empty})[email protected]()" />
                                </div>
                                <div class="m-card-user__details">
                                    <span class="m-card-user__name m--font-weight-500">
                                        @Html.Raw(Model.GetShownLoginName())
                                    </span>
                                </div>
                            </div>
                        </div>
                      
                    </div>
                </div>
            </li>
        </ul>
    </nav>
</div>

如您所见,我两次调用控制器来获取个人资料图片

在这部分代码中

src="@Url.Action("GetProfilePicture", "Profile", new {area = string.Empty})[email protected]()"

我如何只进行一次调用并将结果写入变量中,然后像

src="profileUrl"
一样使用它?

c# asp.net asp.net-mvc asp.net-core
1个回答
0
投票

您可以使用

ViewBag
来获取您的个人资料图片字符串。在您的
Controller
方法中,将值分配给
ViewBag
:

ViewBag.ProfilePicture=GetProfilePicture();

在你的

View

@using Abp.MultiTenancy
@using Abp.Timing
@using DispatcherWeb.Features
@using DispatcherWeb.Web.Areas.App.Models.Layout
@model HeaderViewModel
@{
    Layout = null;
    var isChatEnabled = (AbpSession.MultiTenancySide == MultiTenancySides.Host || IsFeatureEnabled(AppFeatures.ChatFeature));
    string ProfilePicture= ViewBag.ProfilePicture != null ? ViewBag.ProfilePicture : string.Empty;
}

<div id="m_header_topbar" class="m-topbar  m-stack m-stack--ver m-stack--general">
    <nav class="m-stack__item m-topbar__nav-wrapper"
         aria-label="@L("TopMenu")">
        <ul class="m-topbar__nav m-nav m-nav--inline"
            aria-label="@L("TopMenu")"
            role="menubar">
            <li class="m-nav__item m-topbar__user-profile m-topbar__user-profile--img  m-dropdown m-dropdown--medium m-dropdown--arrow m-dropdown--header-bg-fill m-dropdown--align-right m-dropdown--mobile-full-width m-dropdown--skin-light"
                m-dropdown-toggle="click"
                id="UserProfileMenuItem"
            >
                <a href="#" class="m-nav__link m-dropdown__toggle"
                   role="menuitem"
                   aria-haspopup="true"
                   aria-expanded="false"
                   tabindex="0"
                   id="UserProfileMenuItemLink">
                    <span class="m-topbar__username">
                        @if (Model.IsImpersonatedLogin)
                        {
                            <i class="fa fa-reply m--font-danger"></i>
                        }
                        @Html.Raw(Model.GetShownLoginName())
                    </span>
                    <span class="m-topbar__userpic">
                        <img alt="" class="header-profile-picture m--img-rounded m--marginless m--img-centered" src="@ProfilePicture" />
                    </span>
                </a>
                <div class="m-dropdown__wrapper" id="UserProfileDropdown">
                    <span class="m-dropdown__arrow m-dropdown__arrow--right m-dropdown__arrow--adjust"></span>
                    <div class="m-dropdown__inner">
                        <div class="m-dropdown__header m--align-center profile-image-fallback-container">
                            <div class="m-card-user m-card-user--skin-dark">
                                <div class="m-card-user__pic">
                                    <img alt="" class="header-profile-picture m--img-rounded m--marginless m--img-centered" src="@Url.Action("GetProfilePicture", "Profile", new {area = string.Empty})[email protected]()" />
                                </div>
                                <div class="m-card-user__details">
                                    <span class="m-card-user__name m--font-weight-500">
                                        @Html.Raw(Model.GetShownLoginName())
                                    </span>
                                </div>
                            </div>
                        </div>
                      
                    </div>
                </div>
            </li>
        </ul>
    </nav>
</div>
© www.soinside.com 2019 - 2024. All rights reserved.