0

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);
            }

        }
     }
    [...]
 }
John Saunders
  • 160,644
  • 26
  • 247
  • 397
Slinky
  • 5,662
  • 14
  • 76
  • 130
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Feb 19 '14 at 18:20

1 Answers1

1

What it's saying is that List<NewClubSponsor> isn't a model that was generated by your database or your codefirst.

What you really want to do is save individual NewClubSponsor so you need to change each of those object's state. You also only want to change the object state for existing objects. You want to add new ones.

foreach(var sponser in model.Sponsors)
{
      if(sponser.ID <= 0)
      {
         db.Sponsors.Add(sponser);
      }
      else if(db.Entry(sponser).State == EntityState.Detached)
      {
         db.Sponsors.Attach(sponser);
         db.Entry(sponser).State = EntityState.Modified;
      }

}
db.SaveChanges();  
jamesSampica
  • 12,230
  • 3
  • 63
  • 85