Create ViewModel
Create a ViewModel class that represents the data you want to display in the GridView:
csharp
public class StudentViewModel
{
public int StudentId { get; set; }
public string Stu_Firstname { get; set; }
public string Stu_Lastname { get; set; }
public string Stu_FathersName { get; set; }
public string Address { get; set; }
public string City { get; set; }
public string Sex { get; set; }
public string Email { get; set; }
public string ContactNo { get; set; }
}
Configure AutoMapper
Install the AutoMapper package and configure it in your application startup:
csharp_code
public void ConfigureServices(IServiceCollection services)
{
services.AddAutoMapper(typeof(Startup));
}
Create a mapping profile to map the Model to the ViewModel:
csharp
public class MappingProfile : Profile
{
public MappingProfile()
{
CreateMap<Student, StudentViewModel>();
}
}
Modify Controller
In the controller, use AutoMapper to map the Model to the ViewModel:
csharp
public class StudentController : Controller
{
private readonly IMapper _mapper;
public StudentController(IMapper mapper)
{
_mapper = mapper;
}
public ActionResult Index()
{
List<Student> students = GetStudents();
List<StudentViewModel> viewModel = _mapper.Map<List<Student>, List<StudentViewModel>>(students);
return View(viewModel);
}
private List<Student> GetStudents()
{
// Get students from database using stored procedure
}
[HttpPost]
public ActionResult Create(StudentViewModel viewModel)
{
if (ModelState.IsValid)
{
Student student = _mapper.Map<StudentViewModel, Student>(viewModel);
// Save student to database using stored procedure
return RedirectToAction(“Index”);
}
return View(viewModel);
}
}
In this example, the Index
action maps the list of Student
models to a list of StudentViewModel
objects using _mapper.Map
. The Create
action maps the posted StudentViewModel
back to a Student
model before saving to the database.
Modify View
Update the view to use the StudentViewModel
:
xml_code
@model IEnumerable<StudentViewModel>
<table class=”table”>
<tr>
<th>
@Html.DisplayNameFor(model => model.Stu_Firstname)
</th>
<th>
@Html.DisplayNameFor(model => model.Stu_Lastname)
</th>
<th>
@Html.DisplayNameFor(model => model.Stu_FathersName)
</th>
<th>
@Html.DisplayNameFor(model => model.Address)
</th>
<th>
@Html.DisplayNameFor(model => model.City)
</th>
<th>
@Html.DisplayNameFor(model => model.Sex)
</th>
<th>
@Html.DisplayNameFor(model => model.Email)
</th>
<th>
@Html.DisplayNameFor(model => model.ContactNo)
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Stu_Firstname)
</td>
<td>
@Html.DisplayFor(modelItem => item.Stu_Lastname)
</td>
<td>
@Html.DisplayFor(modelItem => item.Stu_FathersName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Address)
</td>
<td>
@Html.DisplayFor(modelItem => item.City)
</td>
<td>
@Html.DisplayFor(modelItem => item.Sex)
</td>
<td>
@Html.DisplayFor(modelItem => item.Email)
</td>
<td>
@Html.DisplayFor(modelItem => item.ContactNo)
</td>
</tr>
}
</table>
This view displays the StudentViewModel
data in a GridView format.By following these steps and using AutoMapper to map the Model to the ViewModel, you can easily bind the GridView to the ViewModel in ASP.NET MVC.