In ASP. net mvc, the "share variable" method in view and partialview

Source: Internet
Author: User

Recently, I am working on some basic parts to think of a problem on the interface:

In ASP. net mvc (razor view engine), if I want to share one _ layout. cshtml among multiple projects, maintenanceProgramAnd unified style are of great help. But the problem is, what should we do if we really want to share different elements between different projects, such as logo and project name. The _ logonpartial. the cshtml file reminds me of a method. define a variable in cshtml and then create a partial. cshtml, In this partial is _ layout. assign values to variables in cshtml. Isn't that enough? Simply, define a variable that is "shared" in layout and partial. Just try it.

The result is that it quickly finds that this does not seem to work, because whether it is layout, view or partial, It is compiled into a class at runtime and an instance of this class is generated, then it is executed in a certain order and there are no instances in the design phase, so it seems unrealistic to define variables. So I thought of viewbag and viewdata again. I tried it and found that I couldn't. viewbag and viewdata in various views (layout and partial) are actually their own, and they have nothing to do with other views, add an element to the viewdata of partial, and you will not be able to find it in loyout.

So what is the relationship between partial and layout? The answer is coming soon. Controller, they must have used the same controller instance when the lifecycle exists! After debugging, we found that during partial execution, the number of elements in the viewdata of partial is different from that in the viewdata of the controller, which means that they did not use the same reference, therefore, add an element to the viewdata of the Controller in partial, and then callCodeBelow, I tried to read the elements in controller. viewdata and expected results were obtained.

Usage example:

 
Html. viewcontext. Controller. viewdata. Add ("test", "ABCD ");

Finally, check msdn and find another way to solve this problem. That is, the webviewpage. Context. Items attribute is used. The context type is httpcontextbase. The items attribute is described as follows:

"When re-writing in a derived class, a key/value set is obtained. This set can be used to organize and share data between the module and the handler during the HTTP request process ."

Tested and used:

 
Context. items ["test"] = "ABCD ";

Shared variables can also be achieved between two views, but I recommend using the first method.

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.