Application method of partial view in asp.net MVC

Time:2019-12-7

Summary: 

In the development experience of asp.net web form, user control is frequently used, which can reduce repetitive code and facilitate page modularization. This concept has also been introduced into asp.net MVC, “partial view“.

What is segment view and when should we use it?

As a developer familiar with asp.net MVC model, they naturally want to create a component with reusable content and code. In web form, we can create a web user control or web server control. But in MVC, we should use segment view. From the perspective of this concept, it should be useful for any scenario.

In the application, the stock market is displayed on each page

In an application, a calendar control is displayed in multiple pages

A login box

Social networking components for multiple pages, such as Facebook’s like button.

Although the partial view of asp.net MVC can be understood as user control theoretically from two different perspectives of syntax and function. Web user controls use viewstate, postbacks, and events in web forms. In MVC segment view, none of the above technology management states are used. As asp.net web user controls do, partial views can mine models in data sharing between your application and other application components.

1. Create a segment view:

In the solution manager, locate the shared folder under views folder, right-click to add a view, and select Create as a partial view, as shown in the figure:

2. Modify segment view:

<p>P element in partial view</p>

3. Reference the distribution view in other views:

@Html.Partial("~/Views/Shared/IncludePart.cshtml");
 <p>P element in the original view</p>

The effect is as shown in the figure:

4. The code of the second reference method is as follows:

@Html.Action("Test","Home")
 <p>P element in the original view</p>

And modify the test action method in the home controller. The code is as follows:


   public ActionResult Test()
   {
    return View("~/Views/Shared/IncludePart.cshtml");
   }

The effect after running is the same as that in 3.

5. You can pass parameters to action. The code is as follows:

@Html.Action("Test", "Home", new { name = "SharpL", id = 1 })
 <p>P element in the original view</p>

Modify the code in the partial view to:

<p>I'm the segment view</p>
<p>Blog name: @ viewbag.name</p>
<p>The blog ID is: @ viewbag.id</p>

Modify the code of test action method in home controller as follows:


public ActionResult Test(string name,int id)
  {
   ViewBag.id = id;
   ViewBag.name = name;
   return View("~/Views/Shared/IncludePart.cshtml");
  }

The effect after operation is as follows:

6. Common views can also be used as partial views

The code is as follows:


@Html.Partial("~/Views/View1.cshtml")

Where view1 is a normal view as follows:

<p>I am the P element in the normal view</p>

The effect is as shown in the figure:

summary

The above is the whole content of this article. I hope that the content of this article has a certain reference learning value for everyone’s study or work. If you have any questions, you can leave a message and exchange. Thank you for your support for developepaar.