i’m having issues trying to implement a forloop in a view using a ViewModel.
I have this ViewModel
public class EventDashboard
{
public int Id {get;set;}
public string Name {get;set;}
public string Email {get;set;}
public string Phone {get;set;}
public ICollection<FighterDetails> FightersFees{ get; set; }
public void LoadFighterDetails(int eventId, int fighterId, aContext ac, bContext bc){
Fighters fighter = ac
.Fighters
.Include(npo => npo.FighterAddress)
.FirstOrDefault(npo => npo.FighterId== fighterId);
this.Name = fighter.Name;
this.Email = fighter.Email;
this.Phone = fighter.Phone;
LoadFees(bc, fighterId);
}
public void LoadFees(bContext bc, int fighterId)
{
var fees = bc.Fees.Where(f => f.fighterId == fighterId);
FightersFees = new List<FighterDetails>();
foreach(var f in fees){
this.FightersFees.Add(new FighterDetails{
//fill the properties
});
}
}
}
public class FighterDetails
{
public string FighterName { get; set; }
public int fighterId { get; set; }
public decimal FighterFee { get; set; }
}
on my view:
<% foreach( var d in Model.Fighters){ %>
<%} %>
but it keeps pushing out that error (on subject). wonder why it happens on viewmodels. any thoughts on how to help me understand this error is very much appreciated.
EDIT:
-
added the rest of the code
-
and the project is currently using ef 4.0. not sure if that will be relevant.
Apparently you can’t use the “Model” in declaring something if you already declared it??
Well, to explain it better, this is what helped me.
Model conflicts with declaration
i was actually declaring Model on the first few edit boxes.
eg.
<%: Html.DisplayFor(Model => Model.FighterName)%>and then call it again on the foreach. The first time you declare it causes that error.