Partial views in MVC

Source: Internet
Author: User

Background:

The project's duration is coming soon, because the background package is very good, we only need to be familiar with the framework, then the background work is a simple code work. Originally thought the most difficult period has passed, but did not expect the front desk is the most difficult.

b/S Foundation is very weak, plus BS project to do less, encountered difficulties can not be avoided. Xia elder sister said to have a flexible interface, the kind of the HTML to add the onclick era has passed. Today, one of the most flexible interfaces-partial view and razor syntax.

Partial view:

Partial views are, to some extent, very similar to user controls. They can all improve the reusability of content or code, but the difference is that the partial view does not need to use technical management states such as ViewState, postbacks, and Events. There is no difference between a partial view and normal view syntax.

Advantages:

1) Simple

2) Reusable

3) Division of labor

First, create a partial view. Right-click in the project to add a view,

Before we introduce how the partial view is used, let's introduce a view engine, the razor that is selected when the partial view is created.

Razor:

Meaning

A modular solution to optimize the generation of HTML, mainly in terms of code. Razor is not a new programming language, but rather a programmer using only existing C#/VB (or other) programming language knowledge to use razor, we just provide a very good template-based markup syntax for generating HTML in your chosen programming language. It has a very smart input hint that can be written in any text editor. You can support unit tests on a view.

In C #, there are a few simple syntax rules:

Code encapsulated in @{...} In

Variables and functions begin with @

Statement ends with a semicolon

[Plain]View PlainCopyprint?
    1. @* Defining Variables strmessage*@
    2. @{
    3. var str= "Hello world!";
    4. var Message = "Razor";
    5. var strmessage=str + Message;
    6. }
    7. @* using a-label display *@
    8. <a> @strMessage </a>


Show Results:

Razor Web pages can be described as HTML pages with two kinds of content: HTML content and Razor code. Use Razor syntax for ASP. NET Web pages with special file extension cshtml using C # Razor syntax) or vbhtml (using VB Razor)

After a brief overview of razor, let's look at the simple display of the partial view. In normal view index, the partial view is called.

Create a controller and establish a addexampartial partial view.

[CSharp]View PlainCopyprint?
  1. GET:/exam/
  2. //corresponding to normal view
  3. Public ActionResult Index ()
  4. {
  5. return View ();
  6. }
  7. //corresponding addexampartial partial view
  8. Public Partialviewresult addexampartial ()
  9. {
  10. return Partialview ("addexampartial");
  11. }

Put them in a folder for easy management.


In normal view index, increase the invocation statement.

[Plain]View PlainCopyprint?
    1. @{html.renderpartial (".. /.. /views/exam/addexampartial ");}

The page that added the exam is a partial view that has been shown and we can reuse it better.



Summarize:

The above is just a simple display of the partial view, does not involve data sharing such as value transfer. In addition, the use of excessive partial views increases server latency and is controlled appropriately. When you meet a very difficult point, it is your weak place, seize the opportunity, this will be your greatest harvest time.

Partial views in MVC

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.