简体   繁体   English

在ASP.NET MVC3中正确使用TempData?

[英]Proper use of TempData in ASP.NET MVC3?

I have an ASP.NET MVC3 application where my action generates a list of ids that I want to make available to a subsequent AJAX request. 我有一个ASP.NET MVC3应用程序,其中我的动作生成一个id列表,我想让后续的AJAX请求可用。 This is so that I can run a long process in the background and poll on it. 这样我就可以在后台运行一个漫长的过程并对其进行轮询。 The list of ids are the necessary input to this long running process. id列表是这个长时间运行过程的必要输入。 I don't want to pass them in the URL as a parameter, because the list could potentially be very long and cause issues in IE. 我不想在URL中将它们作为参数传递,因为列表可能会很长并且会导致IE出现问题。

My Controller 我的控制器

public ActionResult Run()
{
    List<MyObjs> objs = _db.MyObjs.ToList<MyObjs>();

    string uniqueId = Guid.NewGuid().ToString();
    ViewData["UniqueID"] = uniqueId;
    TempData["ObjIdList" + uniqueId] = String.Join(",", objs .Select(o => o.ObjID).ToArray<int>());

    return View(objs);
}            

public void StartProcess(string uid)
{
    string ids = TempData["ObjIdList" + id].ToString().Split(',');
    ...        
}

My View 我的观点

var uniqueId = '@ViewData["UniqueID"]';

$(document).ready(function (event) {
    $('#startProcess').click(function () {
        $.post("/Scheduler/StartProcess", { uid: uniqueId }, function () {
            getStatus();
        });
        event.preventDefault;
    });
});

function getStatus() {
    var r = new Date().getTime(); // cache killer for IE
    var url = '/Scheduler/GetCurrentProgress/' + uniqueId + "?r=" + r;
    $.get(url, function (data) {
        if (data != "100") {
            $('#status').html(data);
            setTimeout(function () { getStatus(); }, 100);
        } else {
            $('#status').html("Done");
        };
    });
}

This is working in my intial test, albeit on my laptop with one concurrent user. 这是我的初步测试,虽然在我的笔记本电脑上有一个并发用户。 Is this safe, or is there a better way to pass this data? 这是安全的,还是有更好的方法来传递这些数据?

Brandon 布兰登

TempData is like ViewData, except that it persists for two successive requests making it useful for things like passing data between two different controller actions TempData与ViewData类似,不同之处在于它持续存在两个连续的请求,这使得它可以用于在两个不同的控制器操作之间传递数据

Jason C 杰森C.

TempData in MVC actually persists until retrieved. MVC中的TempData实际上会持续存在直到被检索到。 As an FYI Tempdata is actually stored in a users SessionState so it is more like SessionData than ViewData 由于FYI Tempdata实际上存储在用户SessionState中,因此它更像SessionData而不是ViewData

Taken from one of my questions responses - MVC3 Controller Action Result 'Remember' Passed-in Id 取自我的一个问题回复 - MVC3控制器操作结果'记住'传入ID

Essentially TempData is like a Session Property - (stored in the SessionState) used for communication between two successive requests to the controller. 本质上,TempData就像一个会话属性 - (存储在SessionState中),用于连续两次向控制器发出请求之间的通信。 As if this is a good or bad practice well in your case I think it would be perfectly fine to pass data to the tempdata but their are other options, hidden fields among them. 好像这对你的情况来说是一个好的或坏的做法,我认为将数据传递给tempdata是完全没问题的,但是它们是其他选项,其中包含隐藏的字段。 Another good link to look at is ASP.NET MVC - TempData - Good or bad practice 另一个很好的链接是ASP.NET MVC - TempData - 好的或坏的做法

The lifetime of TempData is very short.From the current request to the subsequent request. TempData的生命周期非常短。从当前请求到后续请求。 TempData is is using Session to store the data, behind the scenes. TempData是使用Session来存储数据,在幕后。 But the life time is shorter than the regular session variable, that is up to the subsequent request. 但是生命周期比常规会话变量短,这取决于后续请求。

If you are sure you are going to make the ajax call right after the previous call where you set the TempData, you may use that. 如果您确定要在上一次设置TempData的调用之后立即进行ajax调用,则可以使用该调用。 If you want more control, you may keep it in Session variable and destroy the session variable after you use it wherever you wanted n times. 如果你想要更多控制,可以将它保存在Session变量中,并在你想要n次使用它之后销毁会话变量。

仅当您要在ASP .NET MVC中的控制器之间传递值时才使用TempData。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM