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:
the view using multiple submit buttons.
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:
modelstate.clear();
in post. not recommended framework has not been designed way.- use
post-redirect-get
pattern , display validation failure, framework designed (as @stephenmuecke mentions). - if not bothered validation not use htmlhelpers
- use
request.form
values instead , removesomeviewmodel someviewmodel
parameter. wouldn't recommend lose benefits of model binding. - 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
Post a Comment