I'm not able to show web grid contain and insert data form in only one view
I have created a application where on index.cshtml
i'm displaying all employee details.
index.cshtml
<div>
WebGrid Control
<br /><br />
@Html.Partial("_WebGridPartial")
</div>
in which i have create partial view which display data.
Now I have created another view called Create.cshtml
which takes employee details and save into database.
But if i want to show both grid
and create employee form
then how can i use it?
as problem is when i show data it is using @model IEnumerable<MvcGrid.Models.EmpData>
which is of type IEnumerable and inside Create View
for input fields I need to use @model MvcGrid.Models.EmpData
and only one model statement can be used.
You could pass index.cshtml
a ViewModel that contains both your grid data and a ViewModel for an Employee.
e.g.
public class EmployeesViewModel
{
public IEnumerable<EmployeeViewModel> employeesForGrid { get; set; }
public EmployeeViewModel employeeForEdit { get; set; }
}
In your controller...
public ViewResult Index()
{
IEnumerable<Employee> employees = _employeeRepo.GetEmployees();
IEnumerable<EmployeeViewModel> employeesForGrid = MapEmployeesForView(employees);
var model = new EmployeesViewModel()
{
employeesForGrid = employeesForGrid,
employeeForEdit = new EmployeeViewModel();
};
return View(model);
}
Then index.cshtml
:
@model EmployeesViewModel
@{Html.RenderPartial("_WebGridPartial", Model.employeesForGrid);}
<br/><br/>
@{Html.RenderPartial("_Create", Model.employeeForEdit);}