Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to iterate ViewBag or how can I copy the values of viewBag from one Action to another Action
    primarykey
    data
    text
    <p>I have a base Controller like follow</p> <pre><code> public abstract class BaseController { protected ActionResult LogOn(LogOnViewModel viewModel) { SaveTestCookie(); var returnUrl = ""; if (HttpContext != null &amp;&amp; HttpContext.Request != null &amp;&amp; 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 } } </code></pre> <p>and another Controller </p> <pre><code>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); } } </code></pre> <p>the problem is I missed the view bag values in ProceedLogOn action method. I have the values in Logon method in BaseController.</p> <p>How can I copy the values of ViewBag from one Action to another Action?</p> <p>So I can not simply say <code>this.ViewBag=ViewBag;</code></p> <p>because ViewBag doesn't have setter. I was thinking of Iterating through viewbag. I tried <code>ViewBag.GetType().GetFields()</code> and <code>ViewBag.GetType().GetProperties()</code> but they return nothing.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload