TempData不起作用 [英] TempData is not working

查看:255
本文介绍了TempData不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用TempData来存储和从控制器获取值到View。当我使用visual studio运行我的MVC3应用程序时工作正常但是当我在服务器上部署它时它停止工作可以有人帮助解决这个问题吗?提前致谢



正如我在控制器中使用





I am using TempData for storing and fetching values from controller to View. It's working fine when I am running my MVC3 application using visual studio but it stops working when I deploy it on server can anybody help on this issue ? Thanks in advance

As I am using in a controller


TempData["Save"] = "Success";





一个视图





And in a view

@if (TempData["Save"] != null)
{
    <span style="color: red;margin-removed46%;" >Entries saved sucessfully</span> 
}





使用Visual Studio运行时显示正确的值,当我在服务器上部署此控制器并查看时,它不起作用。



当我在本地视觉工作室上运行时hows as span with values成功保存条目成功但当我在服务器上部署视图和控制器时,跨度丢失!!!



It shows proper values when running with Visual studio and Doesn't work when I deploy this controller and view on server.

When I run on local visual studio it shows as span with values Entries saved sucessfully but when i deploy the view and controller on server the span is missing!!!

推荐答案

1。 TempData意味着一个非常短暂的实例,你只能在当前和后续请求中使用





2.由于TempData以这种方式工作,您需要确定下一个请求是什么,并且

重定向到另一个视图是唯一可以保证这一点的时间。



3.因此,使用TempData可靠运行的唯一场景是当你重定向时,这是因为重定向会杀死当前请求,然后创建一个

服务器上的新请求为重定向视图服务。



4.简单地说,Asp.Net MVC TempData字典用于分享

控制器操作之间的数据。



我写了一篇关于此的文章:



http://sampathloku.blogspot.com/2012 / 09 /如何使用的,aspne t-mvc-tempdata-proper.html [ ^ ]





所以不要将TempData用于carrign数据从控制器到View.For你可以使用 ViewBag



控制器



1. TempData is meant to be a very short-lived instance, and you should only use it
during the current and the subsequent requests only.

2. Since TempData works this way, you need to know for sure what the next request will be, and
redirecting to another view is the only time you can guarantee this.

3. Therefore, the only scenario where using TempData will reliably work is when
you are redirecting.This is because a redirect kills the current request , then creates a
new request on the server to serve the redirected view.

4. Simply said, Asp.Net MVC TempData dictionary is used to share data between
controller actions.

I have written a article about this :

http://sampathloku.blogspot.com/2012/09/how-to-use-aspnet-mvc-tempdata-properly.html[^]


So don't use TempData for carrign data from controller to View.For that you can use ViewBag.

Controller

public ActionResult Index()
{
    ViewBag.Name = "Hajan";
    ViewBag.Age = 25;
    return View();
}









查看








View


<p>
    My name is
    <b><%: ViewBag.Name %></b>,
    <b><%: ViewBag.Age %></b> years old.
    <br />
    I like the following colors:
</p>

< br $>




更多信息:



http://weblogs.asp.net/hajan/ archive / 2010/12/11 / viewbag-dynamic-in-asp-net-mvc-3-rc-2.aspx [ ^ ]



我希望这会对你有所帮助。




More info :

http://weblogs.asp.net/hajan/archive/2010/12/11/viewbag-dynamic-in-asp-net-mvc-3-rc-2.aspx[^]

I hope this will help to you.


感谢大家的评论,但这是一个部署问题我在服务器上发布了控制器和视图而被遗忘了在我使用时部署dll使用网站部署!
Thanks all for your comments but it was an deployment issue I posted Controller and views on server and forgotten to deploy dll as I am used to with Website deployments!


这篇关于TempData不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆