ASP. Four ways of passing data from controller to view (i)

Source: Internet
Author: User

Objective

This series begins with an overview of ASP. NET MVC, but also is the basis of the review and further in-depth study, to ensure that a daily publication of the series of articles, about the basic knowledge is no longer described, the garden of the articles abound, this series if there are inappropriate or wrong, please point out and criticize.

Prelude

The new base item is defined under the Models folder in the following class:

     Public classPerson { Public intId {Get;Set; }  Public stringName {Get;Set; }  Public stringblogaddress {Get;Set; }  Public stringDescription {Get;Set; } }

We instantiate this class in the Controller's action method:

            var New Person ()            {                98,                "xpy0928",                " http://www.cnblogs.com/CreateMyself " ,                 " born non-poverty is expensive, you and I can do nothing, the day after tomorrow if not to try to catch the super, but also blame who! They are unreliable and reliable, and who can rely on it! "            };
Mode 1 (ViewData)

We use ViewData to store the instantiated object in the form of a key-value pair, as follows:

viewdata["person"] = p;

In the view we get the stored values from the ViewData and convert them into objects, as follows:

@using asp.net_mvc_1.models;@{    var p = (person) viewdata['person' ];}

We then get the values in the object:

Let's see if we get the value of the object, as follows:

Mode 2 (VIEWBAG)

The controller action method is created with ViewBag to create a dynamic expression, as follows:

Viewbag.person = p;

In the view, we make the following modifications:

@using asp.net_mvc_1.models;@{    var as Person ;}

Get object values as above, we continue to see if, as we expected:

Mode 3 (Model)

We pass the object through the view method returned in the controller, as follows:

 var  p = new   person () {Id  = 98   =  xpy0928   "  =  " http://www.cnblogs.com/createmyself   "  = "   Born non-poverty is expensive, you and I can do nothing, the day after tomorrow if not to try to catch the super, but also blame who! They are unreliable and reliable, and who can rely on it!              };  return  View (p); 

In this view, we will need to get the strongly typed object:

@using ASP.NET_MVC_1.Models; @model person;

The value of the obtained object is obtained through model, as follows:

No doubt the result remains the same:

Mode 4 (TempData)

To understand the literal meaning that we would mistakenly think of as a temporary object, as if it is used once will not be used, is it true? Obviously not, but its life cycle is really short. The object is to pass data from one controller to another method. What do you mean? Let's imagine a scenario where we add a person's information to the controller's info method, and we jump to another method tempdataobject to show that the object has been successfully created.

Let's demonstrate this scenario:

         PublicActionResult Info () {varp =NewPerson () {Id=98, Name="xpy0928", Blogaddress="http://www.cnblogs.com/CreateMyself", Description="born non-poverty is expensive, you and I can do nothing, the day after tomorrow if not to try to catch the super, but also blame who! They are unreliable and reliable, and who can rely on it! "            }; //TODO Add person to databasetempdata["Info"] ="Tip: You have successfully added a piece of data to the database"; returnRedirecttoaction ("Tempdataobject"); }

Create a TempData method that gets the value passed above, as follows:

         Public actionresult tempdataobject ()        {            return  View ();        }

The view that corresponds to the method gets the value passed over:

"info"]

By accessing info, we observe whether the rendered view can get the value when it jumps over to the method:

Of course in the above process this is the use of the data in one action passed through TempData to another action, but we need to note that this is the use of jump to Tempdataobject to display the data correctly, System.NullReferenceException will appear if we refresh the page directly.

Conclusion

The difference is no longer described, the garden has a lot of summary, today is the end.

ASP. Four ways of passing data from controller to view (i)

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.