首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ASP.NET核心5移动虚电路-精选元素中的会话存储,带选件

ASP.NET核心5移动虚电路-精选元素中的会话存储,带选件
EN

Stack Overflow用户
提问于 2021-04-11 17:25:32
回答 1查看 100关注 0票数 0

我想在我的select中添加Sesson Storage with options。我有4个选择。无论用户选择什么,我都希望该选择在当前会话中持续存在,或者直到用户再次更改该选项。

下面是我的控制器操作

代码语言:javascript
复制
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);
        }

这将添加一个过滤器函数。本节将过滤不同类型的对象。

这是我的剃刀页面

代码语言:javascript
复制
<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>

我如何才能使用户的类型选择在会话期间保持不变?

EN

回答 1

Stack Overflow用户

发布于 2021-04-12 09:42:14

ConfigureServices中的AddSession。

代码语言:javascript
复制
services.AddDistributedMemoryCache();
services.AddSession(options =>
{
    options.IdleTimeout = TimeSpan.FromSeconds(10);
    options.Cookie.HttpOnly = true;
    options.Cookie.IsEssential = true;
});

配置中的UseSession。

代码语言:javascript
复制
app.UseSession();

有关更多详细信息,请参阅doc

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67043346

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档