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 に答える