Error rendering Partialview in a View

Asked

Viewed 202 times

0

I have a View and within it I want to render a Partialview. What happens is that the partial is of a model different but that has relationship between them.

I want to show a partial as if it were the index, where I show everything related to Id by passing the Url in action. That one action that receives the Id is the action details, because it shows all the data. And within that action of details, show this partial that relates to the past id.

I had done it before in another project. So I decided to follow the same steps to do it again, only it’s not working.

By rendering the View, an error is already shown:

An Exception of type 'System.Invalidoperationexception' occurred in System.Web.Mvc.dll but was not handled in user code Additional information: The model item passed into the Dictionary is of type 'System.Data.Entity.DynamicProxies.Accavaliacaocomposic_2033e49b3b44e5935b12e9a68fd75a0c5c81f4ab3e35937d92a1c78cb15ace30', but this Dictionary requires a model item of type 'System.Collections.Generic.Ienumerable`1[Meuprojeto.Models.Accavaliacaocomposicaocorporal]'.

And I don’t know how else to do it, because from what I’ve seen it’s all right.

The way I call it partial is: within the controller RetRetorno, have a actionresult who is called PartialAcc, which serves to search the data related to the id passed on url of action details. I’ve tried everything, but it didn’t work.

The codes I have are:

Action Details

 // GET: RetRetorno/Details/5
    public ActionResult Details(int? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }

        //RetRetorno retRetorno = db.RetRetorno.Find(id);

        RetRetorno retRetorno = db.RetRetorno.Include(o => o.AccAvaliacaoComposicaoCorporal).
            Include(p => p.CliCliente).AsNoTracking().FirstOrDefault(f => f.RetId == id);

        if (retRetorno == null)
        {
            return HttpNotFound();
        }

        return View(retRetorno);
    }

Action what I use to call the partial

   public ActionResult PartialAcc(int? id)
    {
        //AccAvaliacaoComposicaoCorporal acc = db.AccAvaliacaoComposicaoCorporal.Include(o => o.CliCliente).AsNoTracking().ToList().FirstOrDefault();

        //return View(db.AccAvaliacaoComposicaoCorporal.Include(o => o.CliCliente).AsNoTracking().ToList().FirstOrDefault());

        var acc = db.AccAvaliacaoComposicaoCorporal.AsEnumerable();

        return View(acc);

    }

In view details

...

<h2>Details</h2>

<div>
<h4>RetRetorno</h4>
<hr />

@Html.Partial("PartialAcc", Model.AccAvaliacaoComposicaoCorporal)

...

The partial I call is like the index itself, but I’ll put the codes here:

@model IEnumerable<MeuProjeto.Models.AccAvaliacaoComposicaoCorporal>

<table class="table table-striped">
    <tr>
        <th>
            @*@Html.DisplayNameFor(model => model.AccPeso)*@
            Peso
        </th>
        <th>
            @*@Html.DisplayNameFor(model =>

...

What am I doing wrong?

1 answer

0


You’re going through it:

@model MeuProjeto.Models.AccAvaliacaoComposicaoCorporal

Your Partial asks for this:

@model IEnumerable<MeuProjeto.Models.AccAvaliacaoComposicaoCorporal>

Change @model for a simple object (no IEnumerable) that should work.

Will stay like this:

@model MeuProjeto.Models.AccAvaliacaoComposicaoCorporal

<table class="table table-striped">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.AccPeso)
            Peso
        </th>
        <th>
            @Html.DisplayNameFor(model => ...
  • But this way I could show all the records? Like say I have 10 records... Without the Ienumerable, it would show?

  • You can either call the Partial ten times (as it is) or call the Partial once, but then the Partial will have to have @model IEnumerable<MeuProjeto.Models.AccAvaliacaoComposicaoCorporal> and then inside it a @foreach (var item in Model). That’s when you decide.

  • So that’s how I’m doing it... with the foreach, and it’s going wrong. I didn’t put it because my partial is too big... But you’re like this

  • Saying it didn’t work out won’t solve anything. If AccAvaliacaoComposicaoCorporal is part of RetRetorno, means that each RetRetorno has one, and only one object of type AccAvaliacaoComposicaoCorporal. Then inside the foreach rendering RetRetorno you put the call from Partial in the way I put it in the answer: @model MeuProjeto.Models.AccAvaliacaoComposicaoCorporal.

  • Calm down... What happens is that a Retretorno can have more than one Accavalicaocomposicaocorporal... Not one for one... Yes 1 Retrorno for many Accavalicaocomposicaocorporal.

  • So it is the second way even, but you are not passing N elements. You are passing only one, otherwise you would not give the error.

  • Even with the actions as they are? In the details including and in the action to render the partial?

  • Yes. There is something wrong with modeling. A debug would solve it well.

  • I swallowed to the point where you have @Html.Partial(...). In the detail action everything is returned straight, only the action that calls the partial is not called..

Show 4 more comments

Browser other questions tagged

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