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)