-1

The model item passed into the dictionary is of type 'System.Data.Entity.DynamicProxies.User_3B687608CE2EBE077B3CAB0EA66E20DFC054F09C758B6620E58A15CACB9B74E5', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1Models.UserPR]'.

Here is my code:

Controller part

public ViewResultBase ListUserPR(int idUser)
        {
            try
            {
                UserPRParameters parameters = new UserPRParameters();
                parameters.IdUser = idReuniao;
                base.SetViewBag(parameters);
                var model = this.UserPRServices.FindByUserPR(parameters);
                this.ViewBag.IdParameters = parameters.IdParameters;
                return this.ListView(model);
            }
            catch (ValidationException exception)
            {
                base.AddValidationErrors(exception);
                return base.PopUpSuccessView();
            }
        }

Service Class

public IEnumerable<UserPR> FindByUserPR(UserPRParameters parameters)
        {
            try
            {
                var query = base.context.UsersPrs.AsQueryable();
                query = query.Where(x => x.IdUser== parameters.IdUser);

                return query
                    .Sort(parameters, x => x.IdUser)
                    .Paginate(parameters);
            }
            catch (Exception exception)
            {
                throw new Exception(this.context.Database.Connection.ConnectionString, exception);
            }
        }

Partial View _ListUserPR inside View "Management"

@model IEnumerable<UserPR>

@{
    var idGrid = "grid" + this.ViewBag.IdParameters ?? string.Empty;

    var user = this.Model.FirstOrDefault();
    int? userId = 0;
    if (null != user)
    {
        userId= user.IdUser;
    }

    var grid = new IBM.Web.Helpers
        .WebGrid(
                //source: this.Model,
                id: idGrid, rowsPerPage: this.RowsPerPage,
                ajaxUpdateContainerId: idGrid
                );
    var columns = new WebGridColumn[] {
        grid.Column("XXXX", UserPRResources.xxx, style: "center"),
        grid.Column("XXXX", UserPRResources.xxx, style: "center"),
        grid.Column("XXXXX", UserPRResources.xxx, style: "center"),
        ....

View Management and your Controller

@model User
@{
    var id = "id" + Guid.NewGuid().ToString().Substring(0, 5);
    this.ViewBag.Title = UserResources.Management;

}
<div class="box-fields">
    @using (Ajax.BeginForm(
        this.DefaultActionCreate,
        "User",
        new DefaultAjaxOptions()
    ))
    {
        @Html.Partial("Validation")
          section">@Resources.ReuniaoResources.ListUser</h2>
            @Html.Partial("ListUserPR")

Controller

public ViewResultBase Management(int id)
        {
            var model = this.Service.Get(id);
            return base.SwitchView(model);
        }

Service

public User Get(int id)
        {
            return this.context.Users.Find(id);
        }

I have two tables

User > FK in UserPR
UserPR not have PK - it is the result of two relationship tables (User and PR)

Thamires Cunha
  • 85
  • 1
  • 2
  • 7
  • 1
    Don't get distracted with the ugly dynamic proxy error. The cause of your error is that you have a `User` instance when you should instead have a `UserPR` instance. But it would help if you told us which line was giving the error. – sstan Sep 24 '15 at 14:05
  • where is the code of Service.Get(id) ? – Overmachine Sep 24 '15 at 14:09
  • i paste code Service.Get(id) – Thamires Cunha Sep 24 '15 at 14:22
  • There is too much code here which makes no sense, for example what is `SwitchView()` in `return base.SwitchView(model);`? The main issue is that the model in `Management.cshtml` is `@model User`, but inside that view you call `@Html.Partial("ListUserPR")` which expects `@model IEnumerable`. Because you do not specify the model in `@Html.Partial()` it passes the current model (`User`) to the partial so you get an exception. –  Sep 24 '15 at 23:11
  • Best guess is that you want `@Html.Action("ListUserPR", someValue)` which will call the `ListUserPR()` method, passing the it `someValue` (which I assume needs to be the ID of the `User` object) –  Sep 24 '15 at 23:14

1 Answers1

2

I think you can disable the proxy creation by

Configuration.ProxyCreationEnabled = false;

in the dbcontext constructor

Kien Chu
  • 4,735
  • 1
  • 17
  • 31
  • This was a nice suggestion for the problem I experienced which I won't go into, but I found the syntax to be a little different. I needed the context added at the front of the statement as in this SO post (https://stackoverflow.com/questions/7111109/should-i-enable-or-disable-dynamic-proxies-with-entity-framework-4-1-and-mvc3) – Alan Jan 22 '18 at 01:17