0

TempData 在 Base 方法中填充,但在代码返回到派生控制器的方法时立即变为 null。

派生控制器编辑操作(发布):

public class ManageItemsController : BaseController
{
        private BaseControllerSingle<Item, ItemViewModel> GetBaseControllerSingle()
        {
            return new BaseControllerSingle<Item, ItemViewModel>(_itemRepository, AreaName, ControllerName);
        }
...

    // POST: /InventoryMgmt/ManageItems/Edit/5
    [HttpPost]
    public ActionResult Edit(ItemViewModel ItemViewModel)
    {
        ItemViewModel = _manageItemsAppServ.SaveOrUpdate(ItemViewModel, CurrentCompanyId);

        return GetBaseControllerSingle().EditPost(
            ItemViewModel, 
            x => x.Id == ItemViewModel.Item.Id && x.CompanyId == CurrentCompanyId
        );
    }

基本控制器编辑操作:

public class BaseControllerSingle<TRepository, TViewModelSingle> : BaseController
        where TRepository : class, IEntity, IAuditStamps, new()
        where TViewModelSingle : class, IEntity, IViewModelSingle<TRepository, TViewModelSingle>, new()
    {
        ...

        public virtual ActionResult EditPost(
        TViewModelSingle viewModel,
        Expression<Func<TRepository, bool>> predicate = null
    )
    {
        if (ModelState.IsValid)
        {
            BaseAppServSingle<TRepository, TViewModelSingle> baseAppServSingle =
                new BaseAppServSingle<TRepository, TViewModelSingle>(_repository);

            ActionConfirmation<int> result = baseAppServSingle.SaveOrUpdate(
                viewModel,
                CurrentUserId,
                predicate
            );

            TempData["message"] = result.Message;

            if (result.WasSuccessful)
            {
                return RedirectToAction("Edit", new { id = result.Value });
            }
        }

        TempData["message"] = "There is invalid data on the form.";
        return View(viewModel);
    }
4

1 回答 1

0

最后我通过从基本控制器继承而不是实例化一个新实例来解决这个问题。我正在创建的新实例一定弄乱了 TempData

于 2013-01-11T06:25:18.900 回答