我在actionfilter中的第一个请求中将值放入TempData.
filterContext.Controller.TempData["value"] = true;
Run Code Online (Sandbox Code Playgroud)
之后第二个请求进来,我检查了值
filterContext.Controller.TempData.ContainsKey("value")
Run Code Online (Sandbox Code Playgroud)
价值就在那里.然后第三个请求进来,我再次检查该值
filterContext.Controller.TempData.ContainsKey("value")
Run Code Online (Sandbox Code Playgroud)
而且价值仍然存在.在第二次请求后,这个值不应该被销毁吗?所有请求都是AJAX请求.
在asp.net MVC中TempData集合的实际用途是什么,我需要该集合的优缺点,我什么时候需要使用它,哪些视图是共享的,或者有关它的任何有用信息,最后如果有人可以告诉我何时使用它而不是ViewData?
提前致谢
CLOSED是ViewData和TempData之间差异的精确副本?
我想在用户单击按钮后进行RedirectToAction.在重定向之前,我将信息存储到变量中.最后,在我重定向到行动之后,我想展示一些有用的信息.我试过这个:
ViewBag.message = "User with ID = " + id + " was changed status to verified.";
Run Code Online (Sandbox Code Playgroud)
但重定向后将刷新数据.有没有其他方法来实现这一目标?
我一直在使用TempData很长时间,并且面临着一些奇怪的问题.我有基本情景:
[HttpPost]
public ActionResult Create(ProductCreateModel newProduct)
{
// create and save product to db
// try upload product to external site
try { UploadProductToEbay(newProduct); }
catch {
TempData["error"] = "error";
return RedirectToAction("Edit", newProduct.Id);
}
...
}
[HttpGet]
public ActionResult Edit(int Id)
{
var error = TempData["error"]; // at this point temp data collection is empty and have no idea why
...
}
Run Code Online (Sandbox Code Playgroud)
上传失败并return RedirectToAction("Edit", newProduct.Id);执行行时会发生此问题.丢失临时数据值的原因可能不是很明显?
更新: 当我使用时
TempData["error"] = "error";
RedirectToAction(...);
Run Code Online (Sandbox Code Playgroud)
在catch块之外一切正常,临时数据值转移到Edit动作.
我有这个代码
[HttpPost]
public ActionResult Index(LoginModel loginModel)
{
if (ModelState.IsValid)
{
// some lines of code . bla bla bla
TempData["loginModel"] = loginModel;
return RedirectToAction("index", "premium");
}
...
}
Run Code Online (Sandbox Code Playgroud)
这个控制器在这里
public ActionResult Index()
{
var loginModel = TempData["loginModel"] as LoginModel;
...
}
Run Code Online (Sandbox Code Playgroud)
现在,当页面加载时,一切似乎都运行正常.但是当我刷新时,一切都搞砸了,它说loginModel就像是null.问题是,我怎么能跟踪当前的登录用户.我启用了表单身份验证.TNX
错误如下
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it …Run Code Online (Sandbox Code Playgroud)