c# - After submitting a form, the value disappered -


before submits form

after submitted form, "only display" values disappeared, knows error?

after submits form error, staff information disappears

in view, use @html.textboxfor , use viewbag.staffid value "readonly" variable, if submit form error, viewbag.staffid value disappears

<div class="form-group">             @html.labelfor(model => model.staffid, "staffid", htmlattributes: new { @class = "control-label col-md-2" })             <div class="col-md-10">                @html.textboxfor(model => model.staffid, new { @value = viewbag.staffid, @readonly = "readonly", @class = "form-control" })                @html.validationmessagefor(model => model.staffid, "", new { @class = "text-danger" })             </div>         </div>          <div class="form-group">             @html.labelfor(model => model.staffinfo.cname, htmlattributes: new { @class = "control-label col-md-2" })             <div class="col-md-10">                 @html.textboxfor(model => model.staffinfo.cname, new { @value = viewbag.cname, @readonly = "readonly", @class = "form-control" })                 @html.validationmessagefor(model => model.staffinfo.cname, "", new { @class = "text-danger" })             </div>         </div>          <div class="form-group">             @html.labelfor(model => model.tdate, htmlattributes: new { @class = "control-label col-md-2" })             <div class="col-md-10">                 @html.editorfor(model => model.tdate, new { htmlattributes = new { @class = "form-control" } })                 @html.validationmessagefor(model => model.tdate, "", new { @class = "text-danger" })             </div>         </div>          <div class="form-group">             @html.labelfor(model => model.edate, htmlattributes: new { @class = "control-label col-md-2" })             <div class="col-md-10">                 @html.editorfor(model => model.edate, new { htmlattributes = new { @class = "form-control" } })                 @html.validationmessagefor(model => model.edate, "", new { @class = "text-danger" })             </div>         </div>          <div class="form-group">             @html.labelfor(model => model.staffid, "staffid", htmlattributes: new { @class = "control-label col-md-2" })             <div class="col-md-10">                @html.textboxfor(model => model.staffid, new { @value = viewbag.staffid, @readonly = "readonly", @class = "form-control" })                @html.validationmessagefor(model => model.staffid, "", new { @class = "text-danger" })             </div>         </div> 

controller code :

[httppost] [validateantiforgerytoken] public actionresult create([bind(include = "staffid, tdate, edate")] trainingrecord trainingrecord) {     if (modelstate.isvalid)     {         db.trainingrecorddbset.add(trainingrecord);         db.savechanges();         return redirecttoaction("search", "trainingrecords", new { id = trainingrecord.staffid });     }      return view(trainingrecord); } 

you bind model after post example

public actionresult mycontroller(){   var model= new mymodel();   return view(model); }  [httppost] public actionresult mycontroller(mymodel model){   model.staffid = 1;   return view(model); } 

Comments

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -