c# - MVC Hidden field via HTML Helper in Form Post issue -


i have issue when using hidden field in mvc form post. when hidden field generated via html helper won't preserve it's value during postback. when using html tag, works.

unfortunately 1 has taken me whole day work out work around.

here i'm doing... (excuse spelling, re-typed code so):

view model

public class someviewmodel {     public int myproperty1 { get; set; }     public int myproperty2 { get; set; }     public int myproperty3 { get; set; } } 

post method

[httppost] [validateantiforgerytoken] public actionresult myactionmethod(someviewmodel someviewmodel, string command) {   ...   ...   // someviewmodel.myproperty1   ...   ... } 

view

@using (html.beginform("myactionmethod", "somecontroller", formmethod.post, new { @class = "form-horizontal", role = "form" })) {     @html.antiforgerytoken()      @html.hiddenfor(m => m.myproperty1)      <div class="col-md-2">         <input type="hidden" value=@model.myproperty1 name="myproperty1" />         <input type="submit" name="command" value="button1" class="btn btn-primary" />         <input type="submit" name="command" value="button2" class="btn btn-success" />     </div>     <div class="col-md-1"></div>     <div class="col-md-1">         <input type="submit" name="command" value="button3" class="btn btn-danger" />     </div>     <div class="col-md-8"></div> } 

in above view code, html helper hidden field (@html.hiddenfor(m => m.myproperty1)) not work. html tag hidden field (input type="hidden" value=@model.myproperty1 name="myproperty1") does work. have 1 or other enabled. both shown here display purposes.

i'd prefer use html helper syntax, can live html tag.

things note:

  1. the view using multiple submit buttons.

  2. the view using partial view. no content in partial view , nothing being done it.

i can't see how these affect issue. thought i'd mention it, in case.

question: can explain why html helper isn't working?


***** update *****

thanks stephen muecke pointing out needed included in question. thank guessing doing couldn't articulate it.

i'm updating view model property in actionmethod(), , when same view re-rendered, view model property doesn't reflect new value. rather keeping it's initial value, , not preserving new value.

although not obvious , found difficult find many articles on subject clarify me in past default behaviour in asp.net mvc following:

if using html helpers , rendering same view in response post assumed responding failed form validation. therefore values before being set in post rendered in view modelstate.

you have few options:

  1. modelstate.clear(); in post. not recommended framework has not been designed way.
  2. use post-redirect-get pattern , display validation failure, framework designed (as @stephenmuecke mentions).
  3. if not bothered validation not use htmlhelpers
  4. use request.form values instead , remove someviewmodel someviewmodel parameter. wouldn't recommend lose benefits of model binding.
  5. use modelstate.remove specific field, again not recommended.

the best article found on article simon ince in 2010:

asp.net mvc’s html helpers render wrong value!

another 1 rick strahl:

asp.net mvc postbacks , htmlhelper controls ignoring model changes


Comments

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -