In this article we are going to see how to create a Partial view in asp.net mvc, Let we take an example of List the Employee details.Html.Partial returns the values as MVCHtmlString to the View .
Controller:
View : Right click the controller and add a View
Partial View: Right click the controller and add a view , select the partial view options in View creating menu
Model:
Output:
From this article you can learn how to create a partial view.
Controller:
[Authorize]
[NoCache]
public class EmployeeController : Controller
{
Models.EmpModel mod = new Models.EmpModel();
public ActionResult
Create()
{
return View();
}
public ActionResult
Index()
{
List<EMPTABLE>
result = mod.GetEmployees();
return View(result);
}
}
View : Right click the controller and add a View
@model IEnumerable<EFSample.Models.EMPTABLE>
@{
ViewBag.Title = "Sample";
}
@section Menu
{
@Html.ActionLink("Create
New", "Create", "Employee")
}
@section security
{
@Html.ActionLink("Log out","Logout","Security")
}
@foreach (var item in Model)
{
@Html.Partial("_employeePartial",
item)
}
Partial View: Right click the controller and add a view , select the partial view options in View creating menu
@model EFSample.Models.EMPTABLE
<table style="border: 2px solid blue; margin: 2px; width: 200px">
<tr>
<td style="color: Blue; width: 80px">
ID :-
</td>
<td style="margin: 2px; color: Pink">@Model.ID
</td>
</tr>
<tr>
<td style="color: Blue; width: 80px">
Name :-
</td>
<td style="margin: 2px; color: Pink">@Model.NAME
</td>
</tr>
</table>
public class EmpModel
{
EmployeeEntities empdb = new
EmployeeEntities();
public List<EMPTABLE> GetEmployees()
{
return empdb.FETCHEMPLOYEES().ToList();
}
public EMPTABLE
GetEmployee(int? id)
{
return empdb.FETCHEMPLOYEE(id).ToList().Single();
}
}
Output:
From this article you can learn how to create a partial view.
No comments:
Post a Comment