0

I have a base Controller like follow

 public abstract class BaseController
    {

        protected ActionResult LogOn(LogOnViewModel viewModel)
        {
            SaveTestCookie();

            var returnUrl = "";
            if (HttpContext != null && HttpContext.Request != null && HttpContext.Request.UrlReferrer != null)
            {
                returnUrl = HttpContext.Request.UrlReferrer.LocalPath;
            }

            TempData["LogOnViewModel"] = viewModel;


            return RedirectToAction("ProceedLogOn", new { returnUrl });
        }

        public ActionResult ProceedLogOn(string returnUrl)
        {
            if (CookiesEnabled() == false)
            {
                return RedirectToAction("logon", "Account", new { area = "", returnUrl, actionType, cookiesEnabled = false });
            }
            var viewModel = TempData["LogOnViewModel"] as LogOnViewModel;

            if (viewModel == null)
            {
                throw new NullReferenceException("LogOnViewModel is not found in tempdata");
            }

            //Do something
            //the problem is I missed the values which are set in the ViewBag
        }
    }

and another Controller

public class MyController : BaseController
    {

        [HttpPost]
        public ActionResult LogOn(LogOnViewModel viewModel)
        {
            // base.LogOn is used in differnet controller so I saved some details in view bag 

            ViewBag.Action = "LogonFromToolbar";
            ViewBag.ExtraData = "extra data related only for this action";

            return base.LogOn(viewModel);
        }

    }

the problem is I missed the view bag values in ProceedLogOn action method. I have the values in Logon method in BaseController.

How can I copy the values of ViewBag from one Action to another Action?

So I can not simply say this.ViewBag=ViewBag;

because ViewBag doesn't have setter. I was thinking of Iterating through viewbag. I tried ViewBag.GetType().GetFields() and ViewBag.GetType().GetProperties() but they return nothing.

Azadeh Khojandi
  • 3,806
  • 1
  • 30
  • 32

2 Answers2

5

ViewData reflects ViewBag
You can iterate the values you've stored like this :

ViewBag.Message = "Welcome to ASP.NET MVC!";
ViewBag.Answer = 42;

foreach (KeyValuePair<string, object> item in ViewData)
{
    // if (item.Key = "Answer") ...
}

This link should also be useful

Nico
  • 234
  • 2
  • 7
-1

I'm afraid I don't have the answer how to copy ViewBag.

However, I would never use ViewBag that way.

ViewBag is some data the Controller gives to the View to render output if someone does not like to use ViewModel for some reasons. The View should never know anything about the Controller but your ViewBag is holding a ActionName ;).

Anyway, the ProceedLogOn action method has pretty much parameters which is ... not a nice code actually so why hesitate to add more parameters which are currently being hold in MyController.Logon ViewBag? Then inside method ProceedLogOn you have what you want.

;)

Van Thoai Nguyen
  • 986
  • 9
  • 22