Displaying Partial View On Condition in Web API

Introduction

This article describes the calling of the partial view on a conditional basis. The partial view is used for displaying the information that depends on the condition. The partial view works on conditions.

The following is the procedure for creating the application.

Step 1

First we create a Web API application as in the following:

  • Start Visual Studio 2013.
  • From the start window select "New Project".
  • From the new project window select "Installed" -> "Visual C#" -> "Web" -> "Visual Studio 2012".
  • Select "ASP.NET MVC 4 Web Application" and click the "OK" button.

    vie.jpg

  • From the "MVC4 project" window select "Web API".

    vie1.jpg

  • Click on the "OK" button.

Step 2

Now in the "HomeController" add the code. This file exists:

  • In the "Solution Explorer".
  • Expand the "Controller" folder.
  • Select the "HomeController".

    vie2.jpg

Add the following code:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. namespace ViewAPI.Controllers  
  7. {  
  8.     public class HomeController : Controller  
  9.     {  
  10.         public ActionResult Index()  
  11.         {  
  12.             return View();  
  13.         }  
  14.         [HttpPost]  
  15.         public ActionResult Index(string viewpage)  
  16.         {  
  17.             if (viewpage=="1")  
  18.             {  
  19.                 ViewBag.Page = "1";  
  20.             }  
  21.             else if(viewpage=="2")  
  22.             {  
  23.                 ViewBag.Page = "2";  
  24.             }  
  25.             return View();  
  26.         }  
  27.     }  
  28. } 

Step 3

Now create a Partial View.

  • In the Controller.
  • Right-click on the "Index".
  • Click on "Add View".

    vie3.jpg

  • Change the name of the View to "ViewPage1".

    vie4.jpg

  • Click the "Add" Button.

Write this line of code:

  1. @{  
  2.     ViewBag.Title = "ViewPage1";  
  3. }  
  4. <h2>ViewPage1 is Display </h2>   

In the same procedure create another partial view and provide these lines of code for it:

  1. @{  
  2.     ViewBag.Title = "ViewPage2";  
  3. }  
  4. <h2>ViewPage2 is display</h2> 

Step 4

In the View provide the following: 

  • In the "Solution Explorer".

  • Expand the "Views folder".

  • Select "Home" -> "Index.cshtml".

    vie5.jpg

Add the following code:

  1. @{  
  2.     ViewBag.Title = "Index";  
  3. }  
  4. @using (Html.BeginForm("Index""Home"))  
  5. {  
  6.     <select name="viewpage">  
  7.         <option value="1"> View Page 1</option>  
  8.         <option value="2"> View Page 2</option>  
  9.     </select>  
  10.     <br /><br />  
  11.     <input type="submit" value="Submit" />  
  12.     if (ViewBag.Page == "1")  
  13.     {  
  14.         @Html.Partial("ViewPage1");  
  15.     }  
  16.     else if (ViewBag.Page == "2")  
  17.     {  
  18.         @Html.Partial("ViewPage2");  
  19.     }  
  20. } 

Step 5

Execute the application:

vie6.jpg

Click on the "Submit" button.

vie7.jpg

Select the ViewPage2 from the dropdown list and click on the "Submit" button.

vie8.jpg


Similar Articles