English 中文(简体)
我如何能够在伙伴关系中积极改变价值(档案名称)。 NET MVC nav条条条条条,使其在所有行动中保持静态?
原标题:how can I dynamically change value(Profile name) on ASP.NET MVC nav bar and keep it static across all actions?

我正在奥特森蒂语工作,在成功标识之后,我想在纳夫酒吧展示用户名称。 我使用了静态的现场来保存用户名称并用@显示用户名称。 观点Bag ,问题在于路线改变观点变得无效。 这就要求我更新在每个行动中都有一个静态的野心。 因此,如果不在每项行动中更新意见书,还没有任何其他方式这样做? 任何避免出现密码重复的方法都将受到赞赏。

主计长: Dash Service

public async Task<IActionResult> Index()
{
        // Get session
        string id = httpContextAccessor.HttpContext.Session.GetString("AccoountId");
        
        // Find user | loggedin user
        var account = await userService.GetAccount(id);
        
        Profile.Firstname = account.Firstname;
        Profile.Lastname = account.Lastname;
    
        ViewBag.Name =  $"{Profile.Firstname.ToString().Substring(0, 1)}.{Profile.Lastname}";
    
        var report = userService.GetReportAsync().Result;
    
        if (report == null)
            return RedirectToAction("UnAuthorize", "Home");
    
        return View(report);
}

观点:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>@ViewData["Title"] - Contact Manager Portal</title>
    <link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
    <link rel="stylesheet" href="~/css/site.css" />
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

    <script>
        $(document).ready(function () {
            $("#openModalBtn").click(function () {
                $("#myModal").modal( show );
            });
        });
    </script>
</head>
<body>
    <header>
        <nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3">
            <div class="container">
                @*<a class="navbar-brand" asp-area="" asp-controller="Home" asp-action="Index">ContactsManager.Web</a>*@

                <div>
                    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".navbar-collapse" aria-controls="navbarSupportedContent"
                            aria-expanded="false" aria-label="Toggle navigation">
                        <span class="navbar-toggler-icon"></span>
                    </button>

                    <div class="navbar-collapse collapse d-sm-inline-flex flex-sm-row-reverse">
                        <ul class="navbar-nav flex-grow-1">
                            <li class="nav-item">
                                <a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Index">Reports</a>
                            </li>

                            <li class="nav-item">
                                <a class="nav-link text-dark" asp-area="" asp-controller="User" asp-action="Index">Clients</a>
                            </li>
                        </ul>
                    </div>
                </div>

                <div style="gap:.9em; display:flex">
                    @using (Html.BeginForm("Search", "User", FormMethod.Get))
                    {
                        <div class="input-group mb-6 d-flex" style="gap:.6em">
                            <input type="text" class="form-control" name="query" placeholder="Search Contacts">
                            <button type="submit" class="btn btn-outline-secondary">Search</button>
                        </div>
                    }

                    <button id="openModalBtn" type="button" class="btn btn-outline-primary">
                        <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-cloud-plus" viewBox="0 0 16 16">
                            <path fill-rule="evenodd" d="M8 5.5a.5.5 0 0 1 .5.5v1.5H10a.5.5 0 0 1 0 1H8.5V10a.5.5 0 0 1-1 0V8.5H6a.5.5 0 0 1 0-1h1.5V6a.5.5 0 0 1 .5-.5" />
                            <path d="M4.406 3.342A5.53 5.53 0 0 1 8 2c2.69 0 4.923 2 5.166 4.579C14.758 6.804 16 8.137 16 9.773 16 11.569 14.502 13 12.687 13H3.781C1.708 13 0 11.366 0 9.318c0-1.763 1.266-3.223 2.942-3.593.143-.863.698-1.723 1.464-2.383m.653.757c-.757.653-1.153 1.44-1.153 2.056v.448l-.445.049C2.064 6.805 1 7.952 1 9.318 1 10.785 2.23 12 3.781 12h8.906C13.98 12 15 10.988 15 9.773c0-1.216-1.02-2.228-2.313-2.228h-.5v-.5C12.188 4.825 10.328 3 8 3a4.53 4.53 0 0 0-2.941 1.1z" />
                        </svg>
                        Upload
                    </button>
                </div>

                <div>

                    <p style="margin:0px">
                        <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-person" viewBox="0 0 16 16">
                            <path d="M8 8a3 3 0 1 0 0-6 3 3 0 0 0 0 6m2-3a2 2 0 1 1-4 0 2 2 0 0 1 4 0m4 8c0 1-1 1-1 1H3s-1 0-1-1 1-4 6-4 6 3 6 4m-1-.004c-.001-.246-.154-.986-.832-1.664C11.516 10.68 10.289 10 8 10s-3.516.68-4.168 1.332c-.678.678-.83 1.418-.832 1.664z" />
                        </svg>
                        @ViewBag.Name

                    </p>
                </div>

            </div>
        </nav>
    </header>

    <div class="container">
        <main role="main" class="pb-3">

            

            @RenderBody()
        </main>
    </div>

    <footer class="border-top footer text-muted">
        <div class="container" style="text-align:center; font-size:.9rem">
            &copy;Copyright Reserved @DateTime.Now.Year | Contact Manager | <a asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
        </div>
    </footer>
    <script src="~/lib/jquery/dist/jquery.min.js"></script>
    <script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
    <script src="~/js/site.js" asp-append-version="true"></script>

    <script src="~/lib/jquery-validation/dist/jquery.validate.min.js"></script>
    <script src="~/lib/jquery-validation-unobtrusive/dist/jquery.validate.unobtrusive.min.js"></script>

    @RenderSection("Scripts", required: false)
</body>
</html>
问题回答

I have used a static field to hold the user s name and display it using @ViewBag, the problem is when a route changes viewbag becomes null.

诚然,你是正确的,你正在取得结果。 Becuase, ViewBag, TempData, View 所有数据都是数据没有持久性的,这意味着一旦您的负荷考虑,数据将不再提供。 因此,一旦你开出网页或改变路线,就把你的标识用户名留在布局网页上。

So is there any other way to do this without updating viewbag in every action?

为了直接回答你的问题,有。 事实上,处理这一问题的方法很多。 但是,你的设想最方便的是:.net core session。 您已经使用<代码>。 HttpContext.Session 因此,你可以为此再开一届会议。

如你所知,在互联网上,我们有

让我们在实践中看一看我们如何能够在你的塞纳罗里使用:

主计长:

在你的控制人中,你可以做如下工作:

public async Task<IActionResult> Index()
{
    var firstName = "Tipe";
    var LastName = "Farid Uddin";

    HttpContext.Session.SetString("UserName", $"{firstName} {LastName}");
    return View();
}

Layout:

为了在您的布局网页上查阅姓名,首先在您的布局网页上排在顶端,包括:

@inject IHttpContextAccessor httpContextAccessor

现更新你的布局网页如下:

<div>
     <p style="margin:0px">
         <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-person" viewBox="0 0 16 16">
             <path d="M8 8a3 3 0 1 0 0-6 3 3 0 0 0 0 6m2-3a2 2 0 1 1-4 0 2 2 0 0 1 4 0m4 8c0 1-1 1-1 1H3s-1 0-1-1 1-4 6-4 6 3 6 4m-1-.004c-.001-.246-.154-.986-.832-1.664C11.516 10.68 10.289 10 8 10s-3.516.68-4.168 1.332c-.678.678-.83 1.418-.832 1.664z" />
         </svg>
        
         @(httpContextAccessor.HttpContext.Session.GetString("UserName") ?? "")
     </p>
 </div>

方案:

在您的方案范围内,c 您应登记中级知识参考书如下:

builder.Services.AddHttpContextAccessor();
builder.Services.AddSession(options =>
{
   
    options.IdleTimeout = TimeSpan.FromMinutes(2);
   
});

以及 利用本届会议:

app.UseSession();

产出:

“entergraph

<>说明: 您可根据您的要求,对本届会议的各种选择进行定制。 正如早些时候告诉你的那样,你试图实现的几种方法。 此外,请,如果你想进一步研究的话,请参考这一官方文件





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签