1

我有一个 MVC 应用程序,它显示数据库中的项目。每个项目都有一个详细信息和一个编辑视图。编辑视图允许用户通过显示值来更改记录。我遇到的问题是我无法获得要保存的更改。

  • 我相信当用户在“索引”视图中单击“编辑”时,第一个编辑操作结果用于显示记录。当用户在编辑视图中按下“保存更改”按钮时,应该调用第二个编辑操作结果。

  • 调试时,我确实发现诸如“item.Item_No_”之类的值没有被视图中的值填充

我从我的控制器提供了代码,

任何帮助将不胜感激,

缺口

        public ActionResult Edit(string itemNo)
    {
        LinnWorksItemViewModel model = new LinnWorksItemViewModel(_data.Connection.ConnectionString, itemNo);
        return View(model);

        // Declare item no here?
            foreach (string upload in Request.Files)
            {
                if (!Request.Files[upload].HasFile()) continue;
                string path = AppDomain.CurrentDomain.BaseDirectory + "uploads/";
                string fileName = Path.GetFileName(Request.Files[upload].FileName);
                Request.Files[upload].SaveAs(Path.Combine(path, fileName));
            }
           return View(model);       
    }

       [HttpPost]
    public ActionResult Edit(Item item, string itemNo)
    {
        LinnWorksItemViewModel model = new LinnWorksItemViewModel(_data.Connection.ConnectionString, itemNo);

        try
        {
            Item _linnItem = _data.Items
                .Where(x => x.Item_No_ == item.Item_No_)
                .FirstOrDefault();
            _linnItem.Description = item.Description;
            _linnItem.Search_Description = item.Extended_Description;
            _linnItem.Default_Barcode = item.Default_Barcode;
            _linnItem.Variations_Group_Code = item.Variations_Group_Code;
            _linnItem.LinnWork_Category_Code = item.LinnWork_Category_Code;
            _linnItem.Unit_Cost = item.Unit_Cost;
            _linnItem.Unit_Price = item.Unit_Price;
            _linnItem.Shipping_Agent_Code = item.Shipping_Agent_Code;
            _linnItem.Package_Group_Code = item.Package_Group_Code;
            _linnItem.Stockkeeping_Units = item.Stockkeeping_Units;
            _linnItem.Weight = item.Weight;
            _linnItem.Width = item.Width;
            _linnItem.Height = item.Height;
            _linnItem.Depth = item.Depth;

            _data.SubmitChanges();
            return RedirectToAction("Index");

        }
        catch (Exception ex)
        {
            string message = ex.Message;
            return View();
        }
    }
4

0 回答 0