entity framework - Relationship field is not updated in Edit action (ASP.NET, EF5) -


i appreciate insight on this: following code

        [httppost]     public actionresult edit(beamcollection beamcollection)     {         if (modelstate.isvalid)         {             beamcollection.beammaterial = db.types.find(convert.toint32(request.form.get("beammaterial_id")));             db.entry(beamcollection).state = entitystate.modified;             db.savechanges();             return redirecttoaction("details", "bridge");         }         return view(beamcollection);     } 

when attempt modify beamcollection record, changes reflected , saved db except beamcollection.beammaterial takes selected value dropdownlist. when debug, can see selected value being assigned beamcollection.beammaterial!

by way, field defined follows

public virtual alltypes beammaterial { get; set; } 

so reflects 1 many relationship alltypes, unidirectional relationship.

what kind of strange (to me), the same technique used create action , works:

    public actionresult create(beamcollection beamcollection)     {         if (modelstate.isvalid)         {             beamcollection.bridgeinfo = db.bridges.find(bridgeid);             beamcollection.beammaterial = db.types.find(convert.toint32(request.form.get("beammaterial_id")));             db.beamcollections.add(beamcollection);             db.savechanges();             return redirecttoaction("details", "bridge");         }         return view(beamcollection);     } 

why happening , how make work, please help.

try use:

db.attach(beamcollection.gettype().name,beamcollection); db.objectstatemanager.changeobjectstate(beamcollection, entitystate.modified); db.savechanges(); 

Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -