我有一个基本的控制器,如下所示
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
}
}
Run Code Online (Sandbox Code Playgroud)
和另一个控制器
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);
}
}
Run Code Online (Sandbox Code Playgroud)
问题是我错过了ProceedLogOn动作方法中的视图包值。我在BaseController的Logon方法中具有值。
如何将ViewBag的值从一个动作复制到另一个动作?
所以我不能简单地说 this.ViewBag=ViewBag;
因为ViewBag没有设置程序。我当时在考虑通过视图袋进行迭代。我尝试过ViewBag.GetType().GetFields(),ViewBag.GetType().GetProperties()但他们什么也没返回。
ViewData反映了ViewBag
您可以像这样迭代存储的值:
ViewBag.Message = "Welcome to ASP.NET MVC!";
ViewBag.Answer = 42;
foreach (KeyValuePair<string, object> item in ViewData)
{
// if (item.Key = "Answer") ...
}
Run Code Online (Sandbox Code Playgroud)
此链接也应该有用
| 归档时间: |
|
| 查看次数: |
2614 次 |
| 最近记录: |