Learn Partial View in MVC razor view engine

0
4505
Partial Views

Partial Views

Hi all, in last topic we learned how to create simple mvc application, today we are going to learn about partial view in mvc razor view engine.

What is Partial View
Like in asp.net we use user control in mvc we use partial views means if you want to reuse your or you want some common functionality to display in different view then we use partial views. Partial view is like a normal regular view with file extension .cshtml.

Advantages of MVC
1) Reusable. Easy to reuse it in different views.
2) Easy to implement, modify.

Now let’s learn how to create simple partial view

1) First step is open visual studio, then click on file->new->project.
2) Pop up will open re-name project to mvcdemo and click ok.
1

3) After clicking another pop up will open select internet application and view engine as razor and click on ok.
2

4) Project is created. Delete all the files in Model, View and Controller folder. Also delete Filter Folder.
3

5) No add new class file under Models folder and rename it to DemoModel and paste below code in it.

4

6) Now right click on Controller folder and add controller and rename it to HomeController and paste below code in it.

5

Learn HTML5 Programming For Beginners

7) Now create Home folder under view folder and then right click on home folder and add view and rename it to Index view and check check box create a strongly typed view and select DemoModel and paste below code in it.

6

7

8) Now again right click on home folder and add view and rename it to _EmployeePartial and check check box create a strongly typed view and select employee model and check check box create a partial view and paste below code in it.
8

9

9) Now you are all done run the website and see the out put.
10

What is difference between Partial, RenderPartial, RenderAction methods

Partial

Html.Partial method return the string output, you can put out put into the variable and manipulate it if required.

RenderPartial

Html.Partial method will directly written to the http response stream

RenderAction

If you want to call action method of partial view then you can use Html.RenderAction method

Summary: In above example we have learnt how to create simple partial view in asp.net using razor view engine.

LEAVE A REPLY

Please enter your comment!
Please enter your name here