c# - Model Key and Foreign Key parametres are null in controller after POST action -


i have following hidden fields inside view:

@html.hiddenfor(model => model.cat.id) @html.hiddenfor(model => model.cat.ownerid) @html.hiddenfor(model => model.cat.birthdate         @html.hiddenfor(model => model.cat.weight) 

id key , ownerid foreignkey. when try edit model, values displayed correctly problem when try post values back. id , ownerid 0. have lot of other values inside view dropdown lists, text boxes etc, values posted correctly. problem id , ownerid.

controller:

[httppost] public actionresult edit(editcatdetailsviewmodel model) { debug.writeline("cat id: " + model.cat.id); //displays 0 } 

i checked source of web site , see id , ownerid have correct values (values aren't 0). question why values not sent other values? why received in controller 0?

thank you!

update:

cat edit view:

@model pro.web.models.editcatdetailsviewmodel  @{     viewbag.title = "edit"; }  <h2>edit cat details</h2>  @using (html.beginform()) { @html.validationsummary(true)  <fieldset>     <legend>cat</legend>      @html.hiddenfor(model => model.cat.id)     @html.hiddenfor(model => model.cat.ownerid)     @html.hiddenfor(model => model.cat.birthdate             @html.hiddenfor(model => model.cat.weight)      <div class="editor-label">         @html.labelfor(model => model.cat.name)     </div>     <div class="editor-field">         @html.editorfor(model => model.cat.name)         @html.validationmessagefor(model => model.cat.name)     </div>      <div class="editor-label">         @html.labelfor(model => model.cat.description)     </div>     <div class="editor-field">         @html.textareafor(model => model.cat.description, new { @style="width:400px; height:600px" })         @html.validationmessagefor(model => model.cat.description)     </div>      <div class="editor-label">         @html.labelfor(model => model.cat.numberofkittens)     </div>     <div class="editor-field">         @html.editorfor(model => model.cat.numberofkittens)         @html.validationmessagefor(model => model.cat.numberofkittens)     </div>      <p>         <input type="submit" value="save" />     </p> </fieldset> }  <div> @html.actionlink("back details", "details") </div> 

at end managed this. put additional variable inside editcatdetailsviewmodel represents cat id.

this simplified of model now:

public class editcatdetailsviewmodel {     public cat cat { get; set; }     public int catid { get; set; }  } 

inside edit method i've set id id in cat model:

// get: /cats/edit/5      public actionresult edit(int id = 0)     {         cat cat = _catrepository.find(id);          if (cat == null)         {             return httpnotfound();         }          editcatdetailsviewmodel model = new editcatdetailsviewmodel();         model.cat = cat;         model.catid = cat.id;          return view(model);     } 

and in view added additional hidden element:

@html.hiddenfor(model => model.catid) 

now can access id in post method:

[httppost] public actionresult edit(editcatdetailsviewmodel model) {     debug.writeline("cat id: " + model.catid); //displays correct id cat!!!     model.cat.id = model.catid; } 

now works , can save changes db. still interested why values not posted first time.


Comments