I have a ViewModel that is retrieving data from a data class.
i have then changed the controller Index method to be able to use the ViewModel which is similar to this example:
namespace WebSite.Models.PassengerViewModels
{
public class PassengerViewModel
{
[Display(Name = "Passenger ID ")]
public int Id { get; set; }
[Display(Name = "Surname ")]
[Required]
public string Surname { get; set; }
[Display(Name = "First Name ")]
[Required]
public string FirstName { get; set; }
[Display(Name = "E-mail Address ")]
[Required]
[DataType(DataType.EmailAddress)]
public string Email { get; set; }
}
}
Then in the controller for Passengers the index i changed to this. i tried adding a filter system aswell for Surname and FirstName:
public async Task<IActionResult> Index(string filter)
{
var passengerContext = from e in _context.Passengers
.Select(a => new PassengerViewModel
{
Id = a.Id,
FirstName = a.FirstName,
Surname = a.Surname,
Email = a.Email,
})
select e;
if (!String.IsNullOrEmpty(filter))
{
passengerContext = passengerContext.Where(e => e.FirstName.Contains(filter)).Where(e => e.Surname.Contains(filter));
}
return View(await _context.Passengers.ToListAsync());
}
Then in the View Index page the code looks like this:
@model IEnumerable<WebSite.Models.PassengerViewModels.PassengerViewModel>
@{
ViewData["Title"] = "Index";
}
<h2>Index</h2>
<p>
<a asp-action="Create">Create New</a>
</p>
<form asp-controller="PassengersController" asp-action="Index">
<p>
Event: <input type="search" name="filter">
<input type="submit" value="Filter" />
</p>
</form>
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.Surname)
</th>
<th>
@Html.DisplayNameFor(model => model.FirstName)
</th>
<th>
@Html.DisplayNameFor(model => model.Email)
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Surname)
</td>
<td>
@Html.DisplayFor(modelItem => item.FirstName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Email)
</td>
<td>
<a asp-action="Edit" asp-route-id="@item.Id">Edit</a> |
<a asp-action="Detail" asp-route-id="@item.Id">Detail</a>
</td>
</tr>
}
</tbody>
</table>
then when i try to enter the page i get this error and i am not sure what its telling me exactly:
InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'System.Collections.Generic.List
1[WebSite.Data.Passenger]', but this ViewDataDictionary instance requires a model item of type System.Collections.Generic.IEnumerable
1[Website.Models.PassengerViewModels.PassengerViewModel]'.
This line is passing data directly from the original context
return View(await _context.Passengers.ToListAsync());
When what you really wanted was
return View(await passengerContext.ToListAsync());
The action should be refactored accordingly
public async Task<IActionResult> Index(string filter) {
var passengers = _context.Passengers
.Select(a => new PassengerViewModel {
Id = a.Id,
FirstName = a.FirstName,
Surname = a.Surname,
Email = a.Email,
});
if (!String.IsNullOrEmpty(filter)) {
passengers = passengers.Where(e =>
e.FirstName.Contains(filter) ||
e.Surname.Contains(filter)
);
}
return View(await passengers.ToListAsync());
}