Wednesday, May 17, 2017

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:

  

No comments:

Post a Comment

Thank you for visiting my blog

Kubernetes

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