asp.net mvc - How do I use an edit viewmodel in MVC? -


i have been struggling time. have model , edit view model can allow user both see image uploaded before , upload replacement. works fine until db.entry portion. error is:

the entity type editcardviewmodel not part of model current context.

if try add editcardviewmodel dbcontext, wants key , table, isn't going happen. viewmodel way pass data. how tell use cards context when saving viewmodel?

controller edit get:

public actionresult edit(int id = 0) {      card card = db.cards.find(id);     viewdata["abilities"] = card.cardabilities.select(a => a.abilityid);      if (card == null)     {         return httpnotfound();     }      var editview = new editcardviewmodel(card);     {     }      return view(editview); } 

controller edit post:

[httppost] public actionresult edit(editcardviewmodel card)  {     if (modelstate.isvalid)     {         if(card.imageupload != null)         {             string savedfilename = path.combine(appdomain.currentdomain.basedirectory, "images");             savedfilename = path.combine(savedfilename, path.getfilename(card.imageupload.filename));             card.imageupload.saveas(savedfilename);             card.cards.imageurl = "\\images\\" + path.getfilename(card.imageupload.filename);         }         db.entry(card).state = entitystate.modified; //error - entity type not part of context         db.savechanges(); 

edit viewmodel:

public class editcardviewmodel {     public card cards { get; set; }     public httppostedfilebase imageupload { get; set; }     public ienumerable<selectlistitem> abilities { get; set; }     public int[] selectedabilities { get; set; }     public ienumerable<selectlistitem> rarities { get; set; }     public int selectedrarities { get; set; }     public ienumerable<selectlistitem> maintypes { get; set; }     public int selectedmaintypes { get; set; }     public ienumerable<selectlistitem> subtypes { get; set; }     public int selectedsubtypes { get; set; }     public ienumerable<selectlist> cardsets { get; set; }     public int selectedcardsets { get; set; }     public rarity rarity { get; set; }     public maintype maintype { get; set; }     public subtype subtype { get; set; }     public cardset cardset { get; set; }      public editcardviewmodel() { } //needed or parameterless constructor error     public editcardviewmodel(card card) //needed or cannot pass card model     {         cards = card;     }  } 

the problem view model not recognised entity framework - has no idea editcardviewmodel meant representation of card. it's bit unclear view model doing either need create new card object , use that:

var newcard = new card {     id = card.id //for example }; 

or possibly use cards property of view model of correct type.


Comments

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -

Nuget pack csproj using nuspec -