razor - ASP.Net MVC 4 Update value linked to a foreign key column -


i'm relatively new mvc 4 , asp.net in general. i'm trying create "room" properties, 1 of queries list of statuses contained in model. have classes set follows:

room.cs

public class room     {         [key]         public guid roomid { get; set; }         [required(errormessage = "a room name required")]         public string name { get; set; }         public string description { get; set; }         public virtual status status { get; set; }         [displayname("created on")]         public datetime? created { get; set; }         [displayname("last modified")]         public datetime? modified { get; set; }     } 

status.cs

public class status     {         [key]         public int statusid { get; set; }         public string name { get; set; }         public string description { get; set; }     } 

when use default scaffolding create action against room, using following code, working flawlessly, , applying status of "new" newly created room.

// post: /room/create

[httppost] [validateantiforgerytoken] public actionresult create(room room)         {             if (modelstate.isvalid)             {                 room.created = datetime.now;                 room.status = db.statuses.single(s => s.name == "new");                 room.roomid = guid.newguid();                 db.rooms.add(room);                 db.savechanges();                 return redirecttoaction("index");             }              return view(room);         } 

when attempt same syntax assigning room.status on edit action, in case, automatically changing status "new" "discovery", status values never modified in database. tracing locals right db.savechanges(); call, , can see room.status value accurately reflecting definition new status, it's not updating on end. here update code:

//room/edit/5

[httppost] [validateantiforgerytoken] public actionresult edit(room room)         {             if (modelstate.isvalid)             {                 room.modified = datetime.now;                 room.status = db.statuses.single(s => s.name == "discovery");                 db.entry(room).state = entitystate.modified;                 db.savechanges();                 return redirecttoaction("index");             }             return view(room);         } 

i'm @ loss how change value! edit view follows:

@model vwr.cloud.models.room  @{     viewbag.title = "edit"; }  <h2>edit</h2>  @using (html.beginform()) {     @html.antiforgerytoken()     @html.validationsummary(true)      <fieldset>         <legend>room - @html.valuefor(model => model.name)</legend>          @html.hiddenfor(model => model.roomid)         @html.hiddenfor(model => model.name)         @html.hiddenfor(model => model.created)         @html.hiddenfor(model => model.status.statusid)          <div class="editor-label">             room status - @html.valuefor(model => model.status.name)         </div>         <div class="editor-label">             created date - @html.valuefor(model => model.created)         </div>         <div class="editor-label">             @html.labelfor(model => model.description)         </div>         <div class="editor-field">             @html.editorfor(model => model.description)             @html.validationmessagefor(model => model.description)         </div>          <p>             <input type="submit" value="save" />         </p>     </fieldset> }  <div>     @html.actionlink("back list", "index") </div> 

it took me way long figure out, needed add [foreignkey] entry room class mapped navigation property. can't yet understand how works, suffice say, :-).

final room.cs

public class room     {         [key]         public guid roomid { get; set; }         [required(errormessage = "a room name required")]         public string name { get; set; }         public string description { get; set; }          [foreignkey("status")]         public virtual int statusid { get; set; }          public virtual status status { get; set; }         [displayname("created on")]         public datetime? created { get; set; }         [displayname("last modified")]         public datetime? modified { get; set; }     } 

Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -