如何坚持MVC电话之间的数据集 [英] How to persist a data set between MVC calls

查看:63
本文介绍了如何坚持MVC电话之间的数据集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下设置这类作品,但它不是正确使用TempData的,并且它的第一次使用后倒下。我怀疑我应该使用的ViewData,但我正在寻找一些指导正确地做到这一点。

The following setup kind of works, but it's not the proper use of TempData, and it falls down after the first use. I suspect I'm supposed to be using ViewData, but I'm looking for some guidance to do it correctly.

在我家的控制器,我有一个指数作用,显示其成员都需要有字母创建:

In my Home controller, I have an Index action that shows which members need to have letters created:

Function Index() As ActionResult
    Dim members = GetMembersFromSomeLongRunningQuery()
    TempData("members") = members
    Return View(members)
End Function

在该索引视图,我有一个链接创建的字母:

On the Index view, I've got a link to create the letters:

<%=Html.ActionLink("Create Letters", "CreateLetters", "Home")%>

早在民政控制器,我有一个CreateLetters函数来处理这样的:

Back in the Home controller, I've got a CreateLetters function to handle this:

Function CreateLetters() As ActionResult
    Dim members = TempData("members")
    For Each member In members
        '[modify member data regarding the letter]
    Next
    TempData("members") = members
    Return RedirectToAction("Letter", "Home")
End Function

最后,还有它创建的实际字母的信图。它的ContentType是应用程序/ msword,所以在Word中的数据实际上来了,而不是浏览器。

Finally, there is a Letter view which creates the actual letters. It's ContentType is "application/msword", so the data actually comes up in Word instead of the browser.

就像我说的,这第一次通过的伟大工程。然而,由于信件来在Word中,当用户回来到浏览器,他们仍然坐在屏幕上的创建快报链接。如果他们再次尝试打它(第一次尝试后,即卡纸),他们得到一个错误,因为CreateLetters TempData的(成员​​),现在是空的。我知道这是TempData的工作方式(单发请求),这就是为什么我试图远离它移动。

Like I said, this works great the first time through. However, since the letters come up in Word, when the user comes back to the browser, they are still sitting on the screen with the "Create Letters" link. If they try to hit it again (i.e. paper jam after the first attempt), they get an error in CreateLetters because TempData("members") is now empty. I know this is how TempData works (single shot requests), which is why I'm trying to move away from it.

这将如何进行重构使用ViewData的?另外,我怎么能同时显示页的信(即Word文档)和重定向应用程序回到指数?

How would this be refactored to use ViewData? Alternatively, how could I simultaneously display the Letter page (i.e. Word document) and redirect the app back to the Index?

推荐答案

我只想再次另一种观点检索对象,但为什么不缓存对象时,它首先检索

I would simply retrieve the object again on the other view but why not cache the object when it is first retrieved

Function Index() As ActionResult
    Dim members = GetMembersFromSomeLongRunningQuery()

// Pseudo code (am a C# man sorry)
    Cache.Remove("MembersItem")
    Cache.Add(members,"MembersItem", CacheExpiry....)

    TempData("members") = members
    Return View(members)
End Function


Function CreateLetters() As ActionResult
    Dim members = Cache.Get("MembersItem")

    For Each member In members
        '[modify member data regarding the letter]
    Next
    TempData("members") = members
    Return RedirectToAction("Letter", "Home")
End Function

您还应该考虑缓存查询的结果为指定的时间或使其依赖于更新。你也可以使用session对象,如果它是特定的用户数据。

You should also consider caching the results of the query for a specified time or make it dependent on an update. You could also use the session object if it is user specific data.

这篇关于如何坚持MVC电话之间的数据集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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