ASP. net mvc uses tempdata

Source: Internet
Author: User

ASP. net MVC tempdata is used to transmit some temporary data, such as transferring temporary data between various controller actions or passing some temporary data to the view. which of the following methods can be used to pass values between pages. in net MVC, tempdata is one of the value passing methods. By default, tempdata uses sessions to store temporary data. The data stored in tempdata is valid only once and is deleted once. This access refers to a request to the next request, because after the next request arrives, the tempdata data stored in the session will be retrieved from the session and assigned to tempdata, then, the data is deleted from the session. Let's take a look at ASP. net mvc preview5 source code:

That is to say, tempdata is saved only to the next request. After the next request is complete, tempdata will be deleted. Note that tempdata uses sessions for storage. Sessions correspond to specific users, so there is no concurrency problem. If you use a database as the storage medium for tempdata, you must consider this situation. As for how to customize the storage medium of tempdata, you can refer to the article "ASP. net mvc: Use db4o for tempdataprovider (a generic redirecttoaction method is attached.

As mentioned above, there is a method in our basecontroller to display the prompt information to the user. This prompt information is temporary information, which can be implemented using tempdata. The following describes how to implement the prompt information:

Protected actionresult showmsg (list <string> MSGs)
{
Tempdata ["messages"] = msgs;
Return redirecttoaction ("message ");
}

Public actionresult message ()
{
Return view (tempdata ["messages"] As list <string> );
}

Because our controllers all inherit from our custom basecontroller, We can display the prompt information in the Controller as follows:

In the tempdata application example, when adding data through tempdata, prevent repeated data submission during page refreshing:

Step 01: Save the data to tempdata. The definition page jumps to the result display page.

Public actionresult save (models. guestbookform data)
{

If (! Modelstate. isvalid)
{
// Verification 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 the temporary data. page Jump prevents repeated submission
Tempdata ["lastpostguestbookform"] = data;

Return redirecttoaction ("result ");

}

Step 2: Create a result action

Public actionresult result ()
{
If (tempdata ["lastpostguestbookform"] = NULL)
{
Return redirecttoaction ("Index ");
}

VaR model = (models. guestbookform) tempdata ["lastpostguestbookform"];

Return view (model );
}

Step 3: Create a 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">

<H2> result </H2>

<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>

 

 

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.