All,
I am trying to save a list of objects in my model and when I step through it, execution lands on
db.Entry(model.Sponsors).State = EntityState.Modified;
and then gets caught:
The entity type List`1 is not part of the model for the current context.
The list is declared in the view model and the data is being pulled into the view OK. It's when I go to save the data that this occurs. How can I fix?
The view that contains the form
for (var i = 0; i < Model.Sponsors.Count; i++)
{
string sponsorDivId = "sponsorclubname" + i.ToString();
string deleteLink = "<a class=\"icon delete cosponsor\" data-attr-divid=\""+@sponsorDivId+"\" data-attr-id=" + @Model.Sponsors[i].Id + "></a>";
<div id="@sponsorDivId">
@Html.HiddenFor(x => x.Sponsors[i].Id)
<div class="formColumn1"><label for="sponsorclubname1">Sponsor club name</label></div>
<div class="formColumn2">@Html.EditorFor(x => x.Sponsors[i].SponsorContactName)
<div class="messageBottom">
@Html.ValidationMessageFor(model => model.Sponsors[i].SponsorContactName)
</div>
</div>
<div class="formColumn3">@Html.EditorFor(x => x.Sponsors[i].SponsorContactEmail)
<div class="messageBottom">
@Html.ValidationMessageFor(model => model.Sponsors[i].SponsorContactEmail)
</div>
</div>
<div class="formColumn4">@Html.EditorFor(x => x.Sponsors[i].SponsorContactPhone)@(i > 0 ? Html.Raw(deleteLink) : Html.Raw(""))
<div class="messageBottom">
@Html.ValidationMessageFor(model => model.Sponsors[i].SponsorContactPhone)
</div>
</div>
</div>
<div class="clear"></div>
}
The declaration of the list in the view model
public List<NewClubSponsor> Sponsors { get; set; }
The controller that does the processing
[HttpPost]
public ActionResult ProcessFormANewClubTeam(FormANewClubTeamViewModel model)
{
if (ModelState.IsValid)
{
try
{
db.Entry(model.Sponsors).State = EntityState.Modified;
db.SaveChanges();
}
catch (Exception ex)
{
{
var err = "Error: " + ex.ToString();
Console.WriteLine("Error: "+ err);
}
}
}
[...]
}