Sunday, 10 September 2017

@Html.Action Example: Render Partial View using @Html.Action in ASP.Net MVC

@Html.Action Example: Render Partial View using @Html.Action in ASP.Net MVC

Description:

In this example we explain that how to Render Partial View using @Html.Action method in asp.net MVC.or how to call partial view using @Html.Action method in MVC.or how to use @Html.Action method to render partial view in razor view in asp.net MVC.or how to render partial view using Action function in MVC Razor.

Controller:

public class EmployeeController : Controller
    {
        // GET: Home
        public ActionResult Index()
        {
            NorthwindEntities entities = new NorthwindEntities();
            return View(from Employee in entities.Employees.Take(10)
                        select Employee);
        }

        [ChildActionOnly]
        public ActionResult Details(string EmployeeId)
        {
            NorthwindEntities entities = new NorthwindEntities();
            return PartialView("Details", entities.Employees.Find(EmployeeId));
        }
    }

 View:

@model IEnumerable<PartialView_Action_MVC.Employee>

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width"/>

    <title>@Html.Action Example: Render Partial View using @Html.Action in ASP.Net MVC

</title>
</head>
<body>
    <h4>Employees</h4>
    <hr/>
    <table cellpadding="0" cellspacing="0" id="EmployeeGrid">
        <tr>
            <th>EmployeeID</th>
            <th>Contact Name</th>
            <th>Address</th>
        </tr>
        @foreach (Employee Employee in Model)
        {
            <tr>
                <td>@Employee.EmployeeID</td>
                <td>@Employee.ContactName</td>
                <td>@Html.Action("Details", new { EmployeeId = Employee.EmployeeID })</td>
            </tr>
        }
    </table>
</body>
</html>

PartialView: 

@model Partial_View_Entity_MVC.Employee

@Html.DisplayFor(model => model.Address)
<br/>
@Html.DisplayFor(model => model.City),
@Html.DisplayFor(model => model.PostalCode)
<br/>
@Html.DisplayFor(model => model.Country)


This entry was posted in :

0 comments:

Post a Comment