Error loading partial view (aspnet core with Dapper)

Asked

Viewed 30 times

0

I am new with the use of Aspnet Core 3.0 with Dapper and I am having difficulties to load a partial view in the main view.

I want to upload client contacts to a list: Client (view) and Contacts (partial view)

Model Clientes:

public class Cliente
{
   [Key]
   public int CodCli { get; set; }        
   public string Nome { get; set; }
   public string Fantasia { get; set; }
   public string CNPJ { get; set; }   

  public IEnumerable<ClienteContato> Contatos { get; set; }

}

Model Contatos:

    public class ClienteContato
    {
        public int FkCodCli { get; set; }
        public string Fantasia { get; set; }    
        public string Cargo { get; set; }     
        public string Nome { get; set; }    
   }

Controller:

public IActionResult Details(int CliId)
        {                  

            using (IDbConnection db = new SqlConnection(_config.GetConnectionString("DefaultConnection")))
            {
                var query = @"SELECT * from VIE_Clientes  WHERE CodCli = @CliId;
                       SELECT * FROM VIE_ClientesContatos WHERE FkCodCli = @CliId";


                var results = db.QueryMultiple(query, new { @CliId = CliId });    

                var clientes = results.ReadSingle<Cliente>();

                if (clientes != null)
                    clientes.Contatos = results.Read<ClienteContato>().ToList();

                return View(clientes);

            }
        }

View Details:

@model SIGApp.Entities.Cliente

    @{
        ViewData["Title"] = "Details";
    }

    <h1>Details</h1>

    <div>
        <h4>Cliente</h4>
        <hr />
        <dl class="row">
            <dt class="col-sm-2">
                @Html.DisplayNameFor(model => model.CodCli)
            </dt>
            <dd class="col-sm-10">
                @Html.DisplayFor(model => model.CodCli)
            </dd>
            <dt class="col-sm-2">
                @Html.DisplayNameFor(model => model.Nome)
            </dt>
            <dd class="col-sm-10">
                @Html.DisplayFor(model => model.Nome)
            </dd>
            <dt class="col-sm-2">
                @Html.DisplayNameFor(model => model.Fantasia)
            </dt>
            <dd class="col-sm-10">
                @Html.DisplayFor(model => model.Fantasia)
            </dd>    
        </dl>
    </div>
    <div>

        <a asp-action="Index">Back to List</a>
    </div>

    <partial name="_teste"/>

Partial View _test

@model IEnumerable<SIGApp.Entities.ClienteContato>

<p>
    <a asp-action="Create">Create New</a>
</p>
<table class="table">
    <thead>
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.FkCodCli)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Fantasia)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Cargo)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Nome)
            </th>
 <th></th>
        </tr>
    </thead>
    <tbody>
@foreach (var item in Model) {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.FkCodCli)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Fantasia)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Cargo)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Nome)
            </td>
 </tr>
}
    </tbody>
</table>

When running the project I get the following error: Invalidoperationexception: The model item passed into the Viewdatadictionary is of type 'Sigapp.Entities.Client', but this Viewdatadictionary instance requires a model item of type 'System.Collections.Generic.Ienumerable`1[Sigapp.Entities.Customerscontact]'.

Thanks in advance for any help.

Thank you very much,

Marcelo

1 answer

0

I figured out what it was!!!

At Model Client I changed to:

 public List<ClienteContato> Contatos { get; set; }

In View missing parameter for popular list:

<partial name="_Teste" for="Contatos">

And in the Main View:

@model List<SIGApp.Entities.ClienteContato>

<td style="text-align:left">@item.Fantasia</td>

That’s it!!

Browser other questions tagged

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