简体   繁体   English

ASP.NET 核心 5 MVC - Session 存储在 select 元素中,带有选项

[英]ASP.NET Core 5 MVC - Session Storage in select element with options

I want to add Sesson Storage in my select with options.我想在我的 select 中添加 Sesson Storage 和选项。 I have 4 options.我有 4 个选项。 And whatever the user choose, I want that choice to persist in current session, or until the user change the option again.无论用户选择什么,我都希望该选择在当前 session 中持续存在,或者直到用户再次更改该选项。

Here is my controller action这是我的 controller 动作

public IActionResult Index(string sortOrder, string filterString)
        {
            //These lines of code creates the filtering for all the types
            ViewData["TypeSortParm"] = String.IsNullOrEmpty(sortOrder) ? "types" : "";
            ViewData["CurrentFilter"] = filterString;
            var types = from t in _context.LibraryItems
                        select t;
            if (!String.IsNullOrEmpty(filterString))
            {
                types = types.Where(t => t.Type.Contains(filterString));
            }

            switch (sortOrder)
            {
                case "types":
                    types = types.OrderByDescending(s => s.Type);
                    break;
                default:
                    types = types.OrderBy(s => s.Category);
                    break;
            }

            var libs = _context.LibraryItems.Include(c => c.Category).ToList();

            var viewModel = new CatagoriesAndLibraryViewModel
            {
                Categories = _context.Categories.ToList(),
                LibraryItems = (filterString != null) ? libs.Where(x => x.Type == filterString).ToList() : libs
            };

            return View(viewModel);
        }

This adds a filter function.这增加了一个过滤器 function。 This section is filtering diffrent types of objects.本节过滤不同类型的对象。

This is my razor page这是我的 razor 页面

<form asp-action="Index" method="get">
    <div>
        <p>
            Find by Type: <select type="text" name="filterString" value="@ViewData["CurrentFilter"]" class="custom-select">
                <option selected>Select a type</option>
                <option value="Book">Book</option>
                <option value="AudioBook">AudioBook</option>
                <option value="ReferenceBook">Reference Book</option>
                <option value="DVD">DVD</option>
            </select>
            <hr />
            <input type="submit" value="Filter" class="btn btn-outline-success" /> |
            <a asp-action="Index" class="btn btn-outline-dark">Back to full list</a>
        </p>
    </div>
</form>

How can I make so the users choice of type will persist during the session?我怎样才能使用户选择的类型在 session 期间持续存在?

AddSession in ConfigureServices.在 ConfigureServices 中添加会话。

services.AddDistributedMemoryCache();
services.AddSession(options =>
{
    options.IdleTimeout = TimeSpan.FromSeconds(10);
    options.Cookie.HttpOnly = true;
    options.Cookie.IsEssential = true;
});

UseSession in Configure.在配置中使用会话。

app.UseSession();

For more details, refer to the doc .有关更多详细信息,请参阅文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM