ASP.NET 核心 MVC - 有什么方法可以更改带有会话的模型上特定数据的值吗?

ASP.NET Core MVC - Is there any way to change the value of specific data on model with session?

有什么方法可以更改模型上特定数据的值吗? 例如我的模型包含

item_id item_description 数量

并且您使用会话在数据模型上有很多记录。 在更改数量期间输入我想用会话更新模型的值? 推荐吗?还是有比我的方法更好的方法?谢谢。

What I was trying to achieve is to modify the data inside the session. Is modifying data on session also working like a database? where you can get the id and modify the data that you want?

根据你的描述,我假设你正在使用 Session 来记录列表,现在你想更新会话数据,对吗?如果是这样的话,你可以参考下面的例子:

  1. 参考this article启用Startup.cs文件中的会话中间件

    [注意]:请记得通过 IdleTimeout 属性 设置会话过期时间。而且,由于您使用会话来存储对象列表,请记住添加 SessionExtensions.

         //required using Microsoft.AspNetCore.Http;
         //required using System.Text.Json;
         public static class SessionExtensions
         {
             public static void Set<T>(this ISession session, string key, T value)
             {
                 session.SetString(key, JsonSerializer.Serialize(value));
             }
    
             public static T Get<T>(this ISession session, string key)
             {
                 var value = session.GetString(key);
                 return value == null ? default : JsonSerializer.Deserialize<T>(value);
             }
         }
    
  2. 添加项目视图模型:

     public class ItemViewModel
     {
         public int ItemId { get; set; }
         public string ItemDescription { get; set; }
         public int Quantity { get; set; }
     }
    
  3. 控制器:我正在使用存储库来设置初始数据。从session中获取数据后,我们可以修改数据,然后调用set方法将最新的数据保存到session中。

     private readonly ApplicationDbContext _context;
     private readonly IDataRepository _repository; 
     public TestController(ApplicationDbContext context, IDataRepository repository)
     {
         _context = context;
         _repository = repository; 
     }
    
     private readonly string SessionKeyName = "itemlist";
     public IActionResult ItemIndex()
     {
         List<ItemViewModel> items = new List<ItemViewModel>();
         //check if the session is exist or not.
         if (HttpContext.Session.Get<List<ItemViewModel>>(SessionKeyName) == default)
         {   //get the initial data.
             items = _repository.GetItemViewModels();
             //set value to the session.
             HttpContext.Session.Set<List<ItemViewModel>>(SessionKeyName, items);
         }
         else
         {
             items = HttpContext.Session.Get<List<ItemViewModel>>(SessionKeyName);
         } 
         return  View(items);
     }
     public IActionResult EditItem(int Id)
     {
         List<ItemViewModel> items = new List<ItemViewModel>();
         //check if the session is exist or not.
         if (HttpContext.Session.Get<List<ItemViewModel>>(SessionKeyName) == default)
         {
             items = _repository.GetItemViewModels();
             //set value to the session.
             HttpContext.Session.Set<List<ItemViewModel>>(SessionKeyName, items);
         }
         else
         {
             items = HttpContext.Session.Get<List<ItemViewModel>>(SessionKeyName);
         }
    
         return View(items.Where(c=>c.ItemId == Id).FirstOrDefault());
     }
     [HttpPost]
     public IActionResult EditItem(ItemViewModel item)
     {
         List<ItemViewModel> items = new List<ItemViewModel>();
         //check if the session is exist or not.
         if (HttpContext.Session.Get<List<ItemViewModel>>(SessionKeyName) == default)
         {
             items = _repository.GetItemViewModels();
             //set value to the session.
             HttpContext.Session.Set<List<ItemViewModel>>(SessionKeyName, items);
         }
         else
         {
             items = HttpContext.Session.Get<List<ItemViewModel>>(SessionKeyName);
         }
    
         //based on the primary key to find the special item,
         var i = items.Where(c => c.ItemId == item.ItemId).FirstOrDefault();
         //update the quantity.
         i.Quantity = item.Quantity;
         //Update the session with the latest data.
         HttpContext.Session.Set<List<ItemViewModel>>(SessionKeyName, items);
         //redirect to the ItemIndex action and reload the Index page.
         return RedirectToAction(nameof(ItemIndex));
     }
    

结果是这样的: