2013-01-11 28 views
0

TempData在基本方法中填充,但只要代码返回到派生控制器的方法就会变为空。TempData null从基本控制器返回后

派生控制器编辑操作(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 
     ); 
    } 

基本控制器编辑操作:

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

如果没有更多上下文,您的问题还不清楚。它会出现你正在实例化另一个控制器..你怎么做?它使用相同的HttpContext吗? –

+0

Hello Simon ...我已经在每个类中添加了更多的细节,它在派生类中显示了我如何实例化基类。不确定你的HttpContext问题。我没有明确地做任何与HttpContext。我以相同的方式设置ViewBag数据,它仍然存在,所以不知道为什么TempData不会......但是,再次,我是一个asp.net开发人员的初级。 –

+0

我不完全确定你要做什么,但存储在'TempData'中的值在一个页面请求后被清除。你可能需要使用'ViewData'来代替。 – HTX9

回答

0

在我通过从基本控制器继承代替实例化新实例解决了这个端部。我创建的新实例必须与TempData混淆