ASP. NET MVC uses TempData

Source: Internet
Author: User

ASP. TempData is used to transfer some temporary data, such as passing temporary data between controller actions or passing some temporary data to the view, I believe everyone has seen "What are the methods of passing values between ASP." This interview question, TempData is one of the methods of passing values in ASP. TempData By default is to use the session to store temporary data, the data stored in the TempData is only valid once access, once the end of the visit will be deleted. This one-time visit refers to a request to the next request, because after the next request arrives, the TempData data that is stored in the session is fetched and assigned to TempData, and then the data is removed from the session. Let's look at the ASP. NET MVC Preview5 source code:

That is, TempData is saved only to the next request, and after the next request is finished, the TempData is deleted. Note that here TempData uses session to do the storage, the session is corresponding to the specific user, so there is no concurrency problem. If you use a database to do tempdata storage media, you have to consider this situation. As for how to customize TempData storage media, you can refer to "ASP. NET MVC: Using db4o to do Tempdataprovider (with a generic Redirecttoaction method)" this article.

As mentioned earlier in our Basecontroller there is a way to display the message to the user, this message is temporary information, we can use TempData to achieve. Here's how to implement this tip:

protected ActionResult showmsg (list<string> msgs)
{
tempdata["Messages"] = msgs;
Return redirecttoaction ("Message");
}

Public ActionResult Message ()
{
Return View (tempdata["Messages"] as list<string>);
}

Because our controller is inherited from our custom basecontroller, so I can be the controller in this way to display the message to the user:

TempData application example, the case where data is added by TempData to prevent page refreshes from repeating data:

STEP01: Save data to TempData, define page jump to result display page

Public ActionResult Save (models.guestbookform data)
{

if (! Modelstate.isvalid)
{
Validation failed
Return redirecttoaction ("Write");
}

MvcStudyDemo.Models.MvcGuestbookEntities db = new models.mvcguestbookentities ();

Db. Addtomessage (New Models.message ()
{

Body = data. Msgname + data. Email,
adminreply = data. Content,
Issecret = False,
Adminreplytime = DateTime.Now,
Createtime = DateTime.Now,
MemberID = 38

});

Db. SaveChanges ();

viewdata["Name"] = data. Msgname;
viewdata["Email" = data. Email;
viewdata["Content"] = data. Content;


return View ();


Save temporary data, page jump prevent duplicate commit
tempdata["Lastpostguestbookform"] = data;

Return redirecttoaction ("Result");


}

STEP2: New Result action

Public ActionResult Result ()
{
if (tempdata["lastpostguestbookform"] = = null)
{
Return redirecttoaction ("Index");
}

var model = (Models.guestbookform) tempdata["Lastpostguestbookform"];

return View (model);
}

STEP3: New Result action view

<%@ page title= "" Language= "C #" masterpagefile= "~/views/shared/site.master" inherits= "System.Web.Mvc.ViewPage <MvcStudyDemo.Models.GuestBookForm> "%>

<asp:content id= "Content1" contentplaceholderid= "titlecontent" runat= "Server" >
Result
</asp:Content>

<asp:content id= "Content2" contentplaceholderid= "maincontent" runat= "Server" >

<fieldset>
<legend>Fields</legend>

<div class= "Display-label" >MsgName</div>
<div class= "Display-field" ><%: Model.msgname%></div>

<div class= "Display-label" >Email</div>
<div class= "Display-field" ><%: Model.email%></div>

<div class= "Display-label" >Content</div>
<div class= "Display-field" ><%: Model.content%></div>

</fieldset>
<p>
<%: Html.ActionLink ("Back to List", "Index")%>
</p>

</asp:Content>

ASP. NET MVC uses TempData

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.