Below are two ViewModels we use in an ASP MVC3 site we're building. In the code below that, I am trying to populate the TradingPartners
IList
property of the AgentIdDetail
property of the AgentWithTraining
variable named bigAgent
.
To better illustrate:
bigAgent
is a AgentWithTraining
AgentWithTraining
has an AgentIdDetail
ICollection
list object as a property
AgentIdDetail
has an IList
list object by the name of TradingPartner
public class AgentWithTraining
{
public Monet.Models.Agent Agent { get; set; }
public ICollection<AgentProdTrainDetail> AgentProdTrainDetails { get; set; }
public ICollection<AgentIdDetail> AgentIdDetails { get; set; }
}
public class AgentIdDetail
{
public string AgentId { get; set; }
public string CompanyCode { get; set; }
public IList<string> TradingPartners { get; set; }
}
The problem is the following line of code:
bigAgent.AgentIdDetails = new AgentIdDetail();
This gives me the error:
Cannot implicitly convert type 'Monet.ViewModel.AgentIdDetail' to 'System.Collections.Generic.ICollection<Monet.ViewModel.AgentIdDetail>'. An explicit conversion exists (are you missing a cast?)
Can someone explain how I need to go about initializing bigAgent.AgentIdDetails
? Below is the full section of code I am using, just in case that is helpful.
//Grab Trading partner information and add it to 'trainlist' object
List<AgentIdToTradingPartner>tradingParter = new List<AgentIdToTradingPartner>();
var symNumToAgId = from s in db.SymetraNumberToAgentId
where s.SymetraNumber == id
select s;
//*******************************************
//This line is causing the problem
//*******************************************
bigAgent.AgentIdDetails = new AgentIdDetail();
foreach (var s in symNumToAgId)
{
AgentIdDetail item = new AgentIdDetail();
item.AgentId = s.AgentId;
item.CompanyCode = s.CompanyCode;
tradingParter = db.AgentIdToTradingPartner
.Where(r => r.AgentId == s.AgentId).ToList();
item.TradingPartners = new List<string>();
foreach (var t in tradingParter)
{
item.TradingPartners.Add(t.TradingPartner.ToString());
}
bigAgent.AgentIdDetails.Add(item);
}
You cannot initilize this way because the AgentIdDetails
is an ICollection<AgentIdDetail>
and not a single instance of AgentIdDetail
.
You have to initilize the collection and then add a new item on this collection. For sample:
// initilize as a List<AgentIdDetail>
bigAgent.AgentIdDetails = new List<AgentIdDetail>();
foreach (var s in symNumToAgId)
{
AgentIdDetail item = new AgentIdDetail();
item.AgentId = s.AgentId;
item.CompanyCode = s.CompanyCode;
tradingParter = db.AgentIdToTradingPartner
.Where(r => r.AgentId == s.AgentId).ToList();
item.TradingPartners = new List<string>();
foreach (var t in tradingParter)
{
item.TradingPartners.Add(t.TradingPartner.ToString());
}
bigAgent.AgentIdDetails.Add(item);
}
As a good pratice, I like to initilize my Collection properties on ViewModels on the constructor, so I do not need to worry about initilize when I fill it, unless I need a new one, for sample:
public class AgentWithTraining
{
public Monet.Models.Agent Agent { get; set; }
public ICollection<AgentProdTrainDetail> AgentProdTrainDetails { get; set; }
public ICollection<AgentIdDetail> AgentIdDetails { get; set; }
public AgentWithTraining()
{
this.AgentProdTrainDetails = new List<AgentProdTrainDetail>();
this.AgentIdDetails = new List<AgentIdDetail>();
}
}