Error in query with Dapper

4

Well, I'm trying to run a query on com with Dapper and get the following error:

  

The model item entered in the dictionary is of type 'System.Collections.Generic.List'1 [FertilityRate.Models.Pais]', but this dictionary requires an item of type 'Fertility Rate.Models.Pais'.

My Pais entity looks like this:

[Table("Pais")]
public class Pais
{
    [Key]
    public Guid PaisId { get; set; }

    [Required]
    public String Nome { get; set; }

    public virtual ICollection<FertilidadePorAno> FertilidadePorAno { get; set; }
}

Controller :

public async Task<ActionResult> Relatorio(Guid id)
{
    if (id == null)
    {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
    }
    var entries = db.Database.Connection.Query<Pais>(@"SELECT * from Pais where PaisId = @id", new { id = id});
    return View(entries);
}

And my View :

@model TaxaDeFertilidade.Models.Pais

@{
    ViewBag.Title = "Relatorio";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Relatorio</h2>

<div>
    <h4>Pais</h4>
    <hr />
    <dl class="dl-horizontal">
        <dt>
            @Html.DisplayNameFor(model => model.Nome)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.Nome)
        </dd>
    </dl>
</div>

Debugging , I saw that object gets populated in controller

But in return of View the problem happens. How do I resolve it?

    
asked by anonymous 17.01.2017 / 17:26

1 answer

4

You have delivered to the View a collection instead of a single item. Try this:

var entries = db.Database.Connection
                         .Query<Pais>(@"SELECT * from Pais where PaisId = @id", new { id = id})
                         .FirstOrDefault();

Note in the image that there is a Count . This indicates collection. The view is asking for Pais not ICollection<Pais>

    
17.01.2017 / 17:30