Wednesday, May 17, 2017

Partial Views Example 4

Create a Controller A & B

























 


public class BController : Controller
    {
        // GET: B
        public ActionResult Index()
        {
            List<B> objlist = new List<B>();
            B objB = new B();
            objB.EmpID = 5;
            objB.EmpName = "Ramkee Gurrala";
            objB.Companyname = "Avent IT Solution";
            objlist.Add(objB);
            objB.ListEmpB = objlist;
            return View(objB);
           
        }
    }


public class AController : Controller
    {
        // GET: A
        public ActionResult Index()
        {
            List<A> objlist = new List<A>();
            A objA = new A();
            objA.PersonAid = 523;
            objA.PaersonAname = "Rajakonda Ashok";
            objA.PersonAage = 0;
            objlist.Add(objA);
            objA.ListPersonA = objlist;
            return View(objA);
        }

    }


Partial Controller.cs

public class PartialController : Controller
    {
        // GET: Partial
        public ActionResult Index()
        {
            List<Partial> objlistcar = new List<Partial>();
            Partial objcar = new Partial();
            objcar.CarID = 1;
            objcar.CarColor = "Rajakonda Uday";
            objcar.CarPrice = 20000;
            objcar.CarType = "sporty";
            objlistcar.Add(objcar);
            // add item in lists
            Partial objcar1 = new Partial();
            objcar1.CarID = 2;
            objcar1.CarColor = "Rajakonda Devaansh";
            objcar1.CarPrice = 20000;
            objcar1.CarType = "Snajsb";


            objlistcar.Add(objcar1);  // add item in list
            objcar.Listcar = objlistcar; // assigning value to CarModel object
            return View(objcar);
            return View();
        }

   


Output:






Partial View Example 3

Create a Controller A  & B




 public class AController : Controller
    {
        // GET: A
        public ActionResult Index(A currentpage)
        {
            return View();
        }
    }

public class BController : Controller
    {
        // GET: B
        public ActionResult Index(B Currentpage)
        {
            return View();
        }
    
        

    }

Create a Model with Name Partial.cs in Model Folder

namespace PartialView2.Models
{
    public class Partial
    {
        public int CarID { get; set; }
        public string CarType { get; set; }
        public int CarPrice { get; set; }
        public string CarColor { get; set; }
        public List<Partial> Listcar { get; set; }

    }

}

Image is shown below:




Go to A controller Index , and write the below code as shown below


@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <h1>This Is A Page</h1>
    <div> 
        @{
            Html.RenderAction("Index", "Partial");
        }
    </div>
</body>

</html>


Image as shown Below


B Controller Index 





Partial Controller.cs


Partial Controller Index




Code 

@model PartialView2.Models.Partial
<table style="background-color: rosybrown; border:dashed;">
    <tr>
        <td colspan="3"><h2>Child View</h2></td>
    </tr>
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.CarID)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.CarType)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.CarPrice)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.CarColor)
        </th>
    </tr>
    @foreach (var item in Model.Listcar)
    {<tr>
            <td>
                @Html.DisplayFor(modelItem => item.CarID)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.CarType)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.CarPrice)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.CarColor)
            </td>
        </tr>
    }

</table>              



Output:

  

Tuesday, May 16, 2017

Render Partial View Example 2

Creating Model


public class CarModel
{
public int CarID { getset; }
public string CarType { getset; }
public int CarPrice { getset; }
public string CarColor { getset; }

public List<CarModel> Listcar { getset; } // List of Car
}
}

Creating Controller

public class CarController : Controller
{
public ActionResult Index()
{
List<CarModel> objlistcar = new List<CarModel>();
            CarModel objcar = new CarModel();
            objcar.CarID = 1;
            objcar.CarColor = "Red";
            objcar.CarPrice = 20000;
            objcar.CarType = "Uday";
            objlistcar.Add(objcar);
            // add item in list
            CarModel objcar1 = new CarModel();
            objcar1.CarID = 2;
            objcar1.CarColor = "Blue";
            objcar1.CarPrice = 20000;
            objcar1.CarType = "Rajakonda Uday Kumar";

            objlistcar.Add(objcar1);  // add item in list
            objcar.Listcar = objlistcar; // assigning value to CarModel object
            return View(objcar); // Returning model
}
}
}

Creating Partial View

@model IEnumerable<WebApplication1.Models.CarModel>
<table style="background-colorrosybrownborder:dashed;">
<tr>
<td colspan="3"><h2>Child View</h2></td>
</tr>
<tr>
<th>
@Html.DisplayNameFor(model => model.CarID)
</th>
<th>
@Html.DisplayNameFor(model => model.CarType)
</th>
<th>
@Html.DisplayNameFor(model => model.CarPrice)
</th>
<th>
@Html.DisplayNameFor(model => model.CarColor)
</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.CarID)
</td>
<td>
@Html.DisplayFor(modelItem => item.CarType)
</td>
<td>
@Html.DisplayFor(modelItem => item.CarPrice)
</td>
<td>
@Html.DisplayFor(modelItem => item.CarColor)
</td>
</tr>
}
</table>



Creating View for Rendering Partial view

Now we have to render partial view inside Index view. For rendering view we are going to use Html Helper class that will be like as shown below

@Html.Partial("Partial View Name" , "Model which is required")
Once we add partial view inside of view that will be like as shown below 


@model  WebApplication1.Models.CarModel

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>View</title>
</head>
<body>
    <div>
        @Html.Partial("View", Model.Listcar)
    </div>
</body>
</html>

Output:

Finally just run application and check output that will be like as shown below. The outer most view is Parent view and the view with Brown color is Partial view (Child view).


Wednesday, November 2, 2016

How to Use Partial View in MVC with Example








Partial view is a reusable view, which can be used as a child view in multiple other views. It eliminates duplicate coding by reusing same partial view in multiple places. You can use the partial view in the layout view, as well as other content views.

To start with, let's create a simple partial view for the following navigation bar for demo purposes. We will create a partial view for it, so that we can use the same navigation bar in multiple layout views without rewriting the same code everywhere.


The following figure shows the html code for the above navigation bar. We will cut and paste this code in a separate partial view for demo purposes.

Create New Partial View:

To create a partial view, right click on Shared folder -> select Add -> click on View..

Note :If a partial view will be shared with multiple views of different controller folder then create it in the Shared folder, otherwise you can create the partial view in the same folder where it is going to be used.
In the Add View dialogue, enter View name and select "Create as a partial view" checkbox and click Add.

We are not going to use any model for this partial view, so keep the Template dropdown as Empty (without model) and click Add. This will create an empty partial view in Shared folder.

Now, you can cut the above code for navigation bar and paste it in _HeaderNavBar.cshtml as shown below:
Partial View: _HeaderNavBar.cshtml

<div class="navbar navbar-inverse navbar-fixed-top">
    <div class="container">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            @Html.ActionLink("Application name", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" })
        </div>
        <div class="navbar-collapse collapse">
            <ul class="nav navbar-nav">
                <li>@Html.ActionLink("Home", "Index", "Home")</li>
                <li>@Html.ActionLink("About", "About", "Home")</li>
                <li>@Html.ActionLink("Contact", "Contact", "Home")</li>
            </ul>
        </div>
    </div>
</div>
Add index and controller as shown below


Index

!DOCTYPE html>
<html>
<head>
 <meta charset="utf-8" />
 <meta name="viewport" content="width=device-width, initial-scale=1.0">
 <title>@ViewBag.Title - My ASP.NET Application</title>
</head>
<body>
@Html.Partial("_HeaderNavBar")
</body>
</html>

Html.Partial():
@Html.Partial() helper method renders the specified partial view. It accept partial view name as a string parameter and returns MvcHtmlString. It returns html string so you have a chance of modifying the html before rendering.
The Html.RenderPartial method writes output directly to the HTTP response stream so it is slightly faster than the Html.Partial method.

The following table lists overloads of the Partial helper method:
Helper MethodDescription
MvcHtmlString Html.Partial(string partialViewName)Renders the given partial view content in the referred view.
MvcHtmlString Html.Partial(string partialViewName,object model)Renders the partial view content in the referred view. Model parameter passes the model object to the partial view.
MvcHtmlString Html.Partial(string partialViewName, ViewDataDictionary viewData)Renders the partial view content in the referred view. View data parameter passes view data dictionary to the partial view.
MvcHtmlString Html.Partial(string partialViewName,object model, ViewDataDictionary viewData)Renders the partial view content in the referred view. Model parameter passes the model object and View data passes view data dictionary to the partial view.
Render Partial View Using jQuery Sometimes we need to load a partial view within a model popup at run time, in this case we can render the partial view using J Query element's load method.
<script type="text/jscript">  
$('#partialView').load('/shared/PartialViewExample’); 
</script> 
View Vs Partial View
ViewPartial View
View contains the layout pagePartial view does not contain the layout page
_viewstart page is rendered before any view is renderedPartial view does not check for a _viewstart.cshtml. We cannot place any common code for a partial view within the _viewStart.cshtml page.
View may have markup tags like html, body, head, title, meta etc.The Partial view is specially designed to render within the view and as a result it does not contain any mark up.
Partial view is more lightweight than the view. We can also pass a regular view to the RenderPartial method.
If there is no layout page specified in the view, it can be considered as a partial view. In razor, there is no distinction between views and partial views as in the ASPX view engine (aspx and ascx).
You can load your partial view using j Query load method. It makes ajax request to controller action method and load output in html control like div.
Add div in index.cshtml file as shown in below and add a script to
load output of action method
<div id="partialviews">
</div>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<script type="text/jscript">    
$(document).ready(function () {       
$("#partialviews").load('/home/GetAddressForjQuery');       
});       
</script>
Add controller action method
public PartialViewResult GetAddressForjQuery(string category)
{
    return PartialView("_address");
}

Kubernetes

Prerequisites We assume anyone who wants to understand Kubernetes should have an understating of how the Docker works, how the Docker images...