I’m the getting following exception in my application and I don’t know how to solve it.
An unhandled exception occurred while processing the request.
InvalidOperationException: The model item passed into the ViewDataDictionary is of type ‘System.Collections.Generic.Dictionary2[ProjetoFinalMVC.Models.Especializacao,System.Collections.Generic.List
1[ProjetoFinalMVC.Models.Consulta]]’, but this ViewDataDictionary instance requires a model item of type ‘System.Collections.Generic.IEnumerable1[System.Linq.IGrouping
2[ProjetoFinalMVC.Models.Especializacao,ProjetoFinalMVC.Models.Consulta]]’.
I have this function in the Service
public async Task<Dictionary<Especializacao, List<Consulta>>> EncontrarGrupoAsync(DateTime? minDate, DateTime? maxDate)
{
var consultas = await EncontrarDataAsync(minDate, maxDate);
var especializacoes = await _contexto.Especializacao.ToListAsync();
var grouping = new Dictionary<Especializacao, List<Consulta>>();
especializacoes.ForEach(e =>
{
var consulta = consultas.Where(c => c.Doutor.Especializacao == e).ToList();
grouping.Add(e, consulta);
});
return grouping;
}
And I call it in the Controller
public async Task<IActionResult> BuscaGrupo(DateTime? minDate, DateTime? maxDate)
{
if (!minDate.HasValue)
{
minDate = new DateTime(DateTime.Now.Year, 1, 1);
}
if (!maxDate.HasValue)
{
maxDate = DateTime.Now;
}
ViewData["minDate"] = minDate.Value.ToString("yyyy-MM-dd");
ViewData["maxDate"] = maxDate.Value.ToString("yyyy-MM-dd");
var resultado = await _consultasService.EncontrarGrupoAsync(minDate, maxDate);
return View(resultado);
}
In the method’s view I have this model
@model IEnumerable<IGrouping<Especializacao,Consulta>>
What can I be doing wrong?
I’ve tried to return a dictionary in the view, but I don’t know how.
If I change the model to
@model Dictionary<Especializacao, List<Consulta>>
I get an error in this line , saying it’s not possible to use a foreach in key values pair
@foreach (var especialiGroup in Model)
@*
@model IEnumerable<IGrouping<Especializacao,Consulta>>
*@
@model Dictionary<Especializacao, List<Consulta>>
@{
ViewData["Title"] = "Pesquisar consultas";
DateTime minDate = DateTime.Parse(ViewData["minDate"] as string);
DateTime maxDate = DateTime.Parse(ViewData["maxDate"] as string);
<h2>@ViewData["Title"]</h2>
<nav class="navbar navbar-inverse">
<div class="container-fluid">
<form class="navbar-form navbar-left" role="search">
<div class="form-group">
<div class="form-group">
<label for="minDate">Data Inicial</label>
<input type="date" class="form-control" name="minDate" value=@ViewData["minDate"] />
</div>
<div class="form-group">
<label for="maxDate">Data Final</label>
<input type="date" class="form-control" name="maxDate" value=@ViewData["maxDate"] />
</div>
</div>
<button type="submit" class="btn btn-primary">Pesquisar</button>
</form>
</div>
</nav>
@foreach (var especialiGroup in Model)
{
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">Especialização @especialiGroup.Key.Nome, Total de consultas: @especialiGroup.Key.TotalDeConsultas(minDate,maxDate)</h3>
</div>
<div class="panel-body">
<table class="table table-striped table-hover">
<thead>
<tr class="bg-secondary">
<th>
Data
</th>
<th>
Valor
</th>
<th>
Doutor
</th>
</tr>
</thead>
<tbody>
@foreach (var item in especialiGroup)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Data)
</td>
<td>
@Html.DisplayFor(modelItem => item.Valor)
</td>
<td>
@Html.DisplayFor(modelItem => item.Doutor.Nome)
</td>
</tr>
}
</tbody>
</table>
</div>
</div>
}
}
2
Answers
you are sending from the controller to view this model
but your view is expecting
probably you should create ViewModel and convert dictionary to ViewModel in BuscaGrupo action
and view
I solved the issue just putting this line inside the foreach
Now it’s working.