Question

TempData is populated in the Base method, but turns to null as soon as the code returns back to the derived controller's method.

Derived Controller Edit Action (Post):

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
        );
    }

Base Controller Edit Action:

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);
    }
Was it helpful?

Solution

In the end I solved this by inheriting from the base controller instead of instantiating a new instance. The new instance I was creating must've messed with the TempData

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top