Error in Dapper query

Asked

Viewed 274 times

4

Well, I’m trying to perform a consultation on Dapper and get the following error:

The model item inserted in the dictionary is of type'System.Collections.Generic.List`1[Taxability Fertility.Models.Parents]', but this dictionary requires an item of type 'Taxability Fertility.Models.Parents.'.

My entity Pais is 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>

Threshing, I saw what object arrives filled in controller

inserir a descrição da imagem aqui

But on the return of View the problem happens. How do I solve it?

  • Your view asks for a Pais and you are sending a collection of Pais

1 answer

4


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

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 a Pais nay ICollection<Pais>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.