我想在我的select中添加Sesson Storage with options。我有4个选择。无论用户选择什么,我都希望该选择在当前会话中持续存在,或者直到用户再次更改该选项。
下面是我的控制器操作
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);
}这将添加一个过滤器函数。本节将过滤不同类型的对象。
这是我的剃刀页面
<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>我如何才能使用户的类型选择在会话期间保持不变?
发布于 2021-04-12 09:42:14
ConfigureServices中的AddSession。
services.AddDistributedMemoryCache();
services.AddSession(options =>
{
options.IdleTimeout = TimeSpan.FromSeconds(10);
options.Cookie.HttpOnly = true;
options.Cookie.IsEssential = true;
});配置中的UseSession。
app.UseSession();有关更多详细信息,请参阅doc。
https://stackoverflow.com/questions/67043346
复制相似问题