Search code examples
foreachasp.net-mvc-4type-mismatch

Foreach type mismatch MVC 4


My question is related to a beginner's topic (sorry for that...). I'm just starting with MVC and at this moment i'm trying to understand the concepts and strucute

I came from webform way of doing things and now i need to simulate the old webform repeater into MVC. Looking for an alternative on the web i discovered that foreach loops are one of the MVC's way of achieving this, correct ?

Then i bit of context (and please, correct me if something i say indicates a wrong understanding of some concept)...

This is my model class created via edmx file

    public partial class qryMonitor
{
    public int Nsu { get; set; }
    public string NomeDaSolicitacao { get; set; }
    public string Grupo { get; set; }
    public string Prioridade { get; set; }
    public string Transito { get; set; }
    public Nullable<int> IdDestinatario { get; set; }
    public string NomeAutor { get; set; }
    public string DepartamentoAutor { get; set; }
    public int IdAutor { get; set; }
    public Nullable<int> IdAnalistaDesignado { get; set; }
    public string NomeAnalistaDesignado { get; set; }
    public Nullable<int> IdSuperiorAnalistaDesignado { get; set; }
    public string NomeSuperiorAnalistaDesignado { get; set; }
    public string Atividade { get; set; }
    public string CodigoRgb1 { get; set; }
    public string DataSolicitacao { get; set; }
    public int MarcarSolicitacao { get; set; }
}

Then i created another class with a single property...

public class ChamadosViewModel
{
    public IEnumerable<qryMonitor> Chamados { get; set; }
}

Now my controller's code

public ActionResult Index()
{   
    EntidadesHelpDesk _dbHelpDesk = new EntidadesHelpDesk();
    ChamadosViewModel viewModel = new ChamadosViewModel()
    {
        Chamados = _dbHelpDesk.qryMonitor
            .ToList()
            .Where(x => x.Transito == "Respondida")
            .Select(x => new qryMonitor
            {
                Nsu = x.Nsu,
                Transito = x.Transito,
                NomeDaSolicitacao = x.NomeDaSolicitacao,
                NomeAutor = x.NomeAutor,
                Prioridade = x.Prioridade,
                DataSolicitacao = x.DataSolicitacao
            })
    };
        return View(viewModel);
}

and finally the view, created via wizard

@model IEnumerable<ServiceDesk.ViewModel.ChamadosViewModel>
@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>
@using (Html.BeginForm())
{
    <p>
        @Html.ActionLink("Create New", "Create")
    </p>
    <table>
        <tr>
            <th></th>
        </tr>

        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ })     |
                    @Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey     */ }) |
                    @Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey     */ })
                </td>
            </tr>
        }
    </table>
}

When i ran those, i get a type mismatch (wich i'm able to comprehend, but not to solve). The complete massage is

"The model item passed into the dictionary is of type'ServiceDesk.ViewModel.ChamadosViewModel', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[ServiceDesk.ViewModel.ChamadosViewModel]'."

I looked for some similar topics on the web... i found few, but i was not able to solve the problem by comparing the examples and the above situation.

One thing i noted is that usually at samples, i saw code like "Model.ForeachData"

and in my case intelisense gets nothing besides the model itself (looks like a group of a group of things and not just like a simple group).

Thanks in advance and sorry for my english (not my main language). Thanks againg for the help.


Solution

  • The model you are passing to your view contains a collection, but you are attempting to iterate directly over the model.

    Change your view code to use the Chamados property.

    @foreach (var item in Model.Chamados)
    

    Then, change your model type in your view:

    @model ServiceDesk.ViewModel.ChamadosViewModel