c# - After submitting a form, the value disappered -
after submitted form, "only display" values disappeared, knows error?
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
Post a Comment