asp.net mvc - Saving a nested collection with MVC 3, EF 4.1, DbContext, and AutoMapper -


i having difficulty getting nested collection (tags in case) commit database after being passed controller. using ef 4.1 dbcontext api, mvc 3, , automapper.

my models:

partial public class proposal     public property id integer     public property memo string     public property entrydate nullable(of date)     public overridable property categorytags icollection(of categorytag) = new hashset(of categorytag)  end class  public class proposalviewmodel      public property id integer     <datatype(datatype.multilinetext)>     public property memo string     <displayformat(applyformatineditmode:=true, dataformatstring:="{0:d}")>     public property entrydate nullable(of date)     public overridable property categorytags icollection(of categorytag) = new hashset(of categorytag) end class 

my view uses code following post add tags: http://jarrettmeyer.com/post/2995732471/nested-collection-models-in-asp-net-mvc-3. tags created adding input elements name attribute set "categorytag[0].id", "categorytag[1].id" etc... , value of inputs valid ids tags in database

lastly, post code:

<httppost()> public function edit(byval pvm proposalviewmodel) actionresult      dim p new proposal      p = automapper.mapper.map(of proposalviewmodel, proposal)(pvm)      if (modelstate.isvalid)         db.entry(p).state = entitystate.modified         db.savechanges()         return redirecttoaction("index")     else         return view(pvm)     end if  end function 

the pvm object returned controller values set want them be. if add 2 tags in run-time, has 2 categorytag objects in it's collection appropriate ids. problem on call savechanges, corresponding records not created in many-to-many relation table (proposalcategorytag in case).

any idea i'm doing wrong?

update:

i did not find on this, , have resorted doing manually below. dislike method, works.

<httppost()> public function edit(byval pvm proposalviewmodel) actionresult      dim p new proposal     dim temptag categorytag      p = automapper.mapper.map(of proposalviewmodel, proposal)(pvm)      if (modelstate.isvalid)          db.proposals.attach(p)         db.entry(p).collection("categorytags").load()          each ct categorytag in pvm.tags              temptag = db.categorytags.find(ct.id)              if temptag nothing                 continue             end if              if p.categorytags.contains(temptag)                 continue             end if              p.categorytags.add(temptag)         next          db.entry(p).state = entitystate.modified         db.savechanges()         return redirecttoaction("index")     else          return view(pvm)      end if  end function 

update:

i did not find on this, , have resorted doing manually below. dislike method, works.

    <httppost()>     public function edit(byval pvm proposalviewmodel) actionresult          dim p new proposal         dim temptag categorytag          p = automapper.mapper.map(of proposalviewmodel, proposal)(pvm)          if (modelstate.isvalid)              db.proposals.attach(p)             db.entry(p).collection("categorytags").load()              each ct categorytag in pvm.tags                  temptag = db.categorytags.find(ct.id)                  if temptag nothing                     continue                 end if                  if p.categorytags.contains(temptag)                     continue                 end if                  p.categorytags.add(temptag)             next              db.entry(p).state = entitystate.modified             db.savechanges()             return redirecttoaction("index")         else               return view(pvm)          end if      end function 

Comments