Asp.net mvc临时数据

时间:2016-09-19 12:35:45

标签: asp.net-mvc

我有一个动作方法,我设置我的临时数据变量

[HttpGet]
public ActionResult Index1()
{
    if (TempData["Name"] == null)
    {
        TempData["Name"] = "Vinay";
    }
    return View();
}

我还有相应的视图,我使用keep方法将数据保存到tempdata

@using (Html.BeginForm("Index1", "Employee", FormMethod.Post, new { @class = "navbar-right" }))
{
    <h1>
        <h1>@TempData["Name"]</h1>
        @{ TempData.Keep("Name"); }
    </h1>
    <input type="submit" />
}

第一次正常工作但是当我按f5再次刷新页面时,TempData["Name"]变为空。为什么呢?

2 个答案:

答案 0 :(得分:0)

总是,我已经测试过了。因为在刷新(F5)时它再次调用Index1操作方法并再次存储tempdata值并使用tempdata加载视图。

如果您在视图之间遍历而不刷新,则可能会丢失tempdata值

答案 1 :(得分:0)

温度数据示例(索引):-

$output_string = "Test Message";
$headers = "From: online-sales@acompanyname.com \r\n";
$headers .= "Reply-To: info@acompanyname.com \r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
mail("dave.emsley@mycompany.co.uk","Purchase from acompanyname.com",$output_string,$headers);
mail("dave@mycompany.org.uk","Purchase from acompanyname.com",$output_string,$headers);

在控制器中编辑:-

      if (RouteId != null)
        {
            HttpContext.Session.SetString("RouteId", RouteId);
        }
        else
        {
            RouteId = HttpContext.Session.GetString("RouteId");

            if (RouteId == null)
            {
                //return to the View and display an error message
                TempData["message"] = "Please select a route.";
                return RedirectToAction("Index", "BusRoute");
            }
        }
        var routeStopViewModel = from routeStop in _context.RouteStop.Include(rs => rs.BusRouteCode)
                                 select new RouteStopModel
                                 {
                                     BusRouteCode = routeStop.BusRouteCode
                                 };


        var routeStopsContext = _context.RouteStop
            .Include(rs => rs.BusStopNumberNavigation)
            .Where(predicate: rs =>rs.BusRouteCode==RouteId)
            .OrderBy(keySelector: rs => rs.OffsetMinutes).ToListAsync();