asp.net mvc 2 - Relations are not saved when updating the model using EF4 code-first -
like title says successful update, won't save relations user-role or whatever. works great when add user (in case) roles , all, update doesn't work, that's relation else updated. have problem other objects well. have idea why won't save relation if it's changed on update? posted code too, have feeling it's not important here. think i'm missing simple.
if (modelstate.isvalid) { //hämta användaren och redigera parametrar var user = _userservice.getbyid(viewmodel.user.id); viewmodel.user.roles = user.roles; //kolla språken if (viewmodel.cultureselected != null) { foreach (var item in viewmodel.cultureselected) { viewmodel.user.languagecultures.add(_languagecultureservice.getbyid(item)); } } //updatera och spara tryupdatemodel(user, "user"); _userservice.save(); viewdata[message] = ""; return redirecttoaction("lista"); }
edit
in case, ended adding languagecultures
user
directly instead of viewmodel.user
:
public actionresult redigera(adminuseraddviewmodel viewmodel) { //kolla om state är valid if (modelstate.isvalid) { //hämta användaren och redigera parametrar var user = _userservice.getbyid(viewmodel.user.id); //kolla språken if (viewmodel.cultureselected != null) { foreach (var item in viewmodel.cultureselected) { user.languagecultures.add(_languagecultureservice.getbyid(item)); } } //updatera och spara tryupdatemodel(user, "user"); _userservice.save(); viewdata[message] = ""; return redirecttoaction("lista"); } viewmodel.cultureselectlist = cutlureselectlist(_languagecultureservice.getall()); return view(viewmodel); }
i'm still mvc beginner think tryupdatemodel(user, "user")
looks http post values prefixed "user.
" string. think uses directly posted data, not viewmodel instance.
Comments
Post a Comment