I am using entity framework in MVC. I have two models that populate from a SQL table using DBContext
public class Incident
{
public Guid IncidentKey { get; set; }
public int IncidentID { get; set; }
}
public class Request
{
public Guid RequestKey { get; set; }
public int RequestID { get; set; }
}
Below is how I am getting the the data to pull from SQL
public class CallViewerDbContext : DbContext
{
public DbSet<Incident> Incident { get; set; }
public DbSet<Request> Request { get; set; }
}
public class SqlData : IIncident, IRequest
{
private readonly CallViewerDbContext db;
public SqlData(CallViewerDbContext db)
{
this.db = db;
}}
internal static void RegisterContainer(HttpConfiguration httpConfiguration)
{
var builder = new ContainerBuilder();
builder.RegisterControllers(typeof(MvcApplication).Assembly);
builder.RegisterApiControllers(typeof(MvcApplication).Assembly);
builder.RegisterType<SqlData>()
.As<IIncident>()
.InstancePerRequest();
builder.RegisterType<SqlData>()
.As<IRequest>()
.InstancePerRequest();
builder.RegisterType<CallViewerDbContext>().InstancePerRequest();
var container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
httpConfiguration.DependencyResolver = new AutofacWebApiDependencyResolver(container);
}
I am trying to create a new model that will will allow me to get a count for both of the entity’s when I put it on a new Razor view. I am currently able to get one by using the below method.
IIncident db;
public HomeController(IIncident db)
{
this.db = db;
}
public ActionResult Index()
{
var model = db.IGetAll();
return View(model);
}
public IEnumerable<Incident> IGetAll()
{
return db.Incident;
}
I have tried making a new model.
public class DataModel
{
public List<Incident> allIncidents { get; set; }
public List<Request> allRequests { get; set; }
}
I tried adding it to the Register Container
builder.RegisterType<SqlData>()
.As <IDataModel>()
.InstancePerRequest();
Then updated the Home controller
IDataModel db;
public HomeController(IDataModel db)
{
this.db = db;
}
public ActionResult Index()
{
var model = db.MGetAll();
return View(model);
}
public IEnumerable<Incident> MGetAll()
{
return db.Incident;
}
I am trying to return the data using the MGetAll method to get the list of Incidents, I then have a another method that does the same for requests. I was trying to return individual counts to the view first then add in the second.
I am now getting the below stack trace error
The model item passed into the dictionary is of type ‘System.Data.Entity.DbSet1[CallViewerData.Models.Incident]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable1[CallViewerData.Models.DataModel]’.
Any help would be greatly appreciated.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Something like this should work:
public ActionResult Index()
{
var model = PrepareViewModel();
return View(model);
}
private DataModel PrepareViewModel()
{
return new DataModel
{
allIncidents = db..., // Get Incidents
allRequests = db... // Get Requests
};
}
And in your view
@model namespace.DataModel
Then you can access them with Model.allIncidents and Model.allRequests.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0