0

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);
    }
crichavin
  • 4,672
  • 10
  • 50
  • 95
  • Your issue is unclear without more context. It would appear you're instantiating another Controller.. how are you doing that? Does it use the same HttpContext? – Simon Whitehead Jan 11 '13 at 02:27
  • Hello Simon...I have added more detail from each class which shows in the derived class how I am instantiating the base class. Not sure about your HttpContext question. I am not explicitly doing anything with the HttpContext. I am setting ViewBag data in the same way and it persists, so not sure why TempData does not...but then again, I am quite junior of a asp.net developer. – crichavin Jan 11 '13 at 03:22
  • I'm not exactly sure what you are trying to do, but values stored in `TempData` are cleared after one page request. You probably need to use `ViewData` instead. – HTX9 Jan 11 '13 at 04:59
  • @HTX9 thanks for your input, but I don't believe that is correct. ViewData dies upon a RedirectToAction...TempData is specifically meant to last for one subsequent request. http://rachelappel.com/when-to-use-viewbag-viewdata-or-tempdata-in-asp.net-mvc-3-applications – crichavin Jan 11 '13 at 05:26

1 Answers1

0

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

crichavin
  • 4,672
  • 10
  • 50
  • 95