asp.net mvc - MVC 4 FormCollection not updated on resubmit -


here facts of issue:

  1. i have form in asp.net mvc 4 web application.
  2. the fields on form not tied explicit static properties within model. instead dynamic fields can change.
  3. when form submitted, use formccollection retrieve values entered field values.
  4. the first time submit form, well: values of formcollection accurately reflect form values.
  5. if there problem field values due 1 or more invalid fields, redisplay form.
  6. if user changes values of form correct validation errors , resubmits same form, formcollection not updated reflect latest form values. instead contains field values first submit.

why happening , how can correct it?

<httppost()> _ <httpparamaction()> _ function upload(byval model maxdocument, formcollection formcollection) actionresult   dim scriteria string = ""   dim nkeyindex integer = 0   dim nfieldindex integer = -1   dim sfieldvalue string = ""    try     ' build scriteria submitted formcollection     model.getfilecabinetfieldlist()     nfieldindex = 0 (model.indexfieldcount - 1)       sfieldvalue = ""       if nfieldindex > 0         scriteria += "~"       end if       dim fcf maxserverlib.filecabinetfield = model.criterionatindex(nfieldindex)         ' field value corresponding field         each okey object in formcollection.allkeys           if okey.tostring = fcf.sfieldname             sfieldvalue = formcollection(okey.tostring)             exit           end if         next         scriteria += sfieldvalue       next       if scriteria = "" scriteria = "[blankindex]"        ' set criteria property of model, used both field validation , document export.       model.criteria = scriteria        ' first thing perform valiation of criteria       model.validatefieldvalues()       if not model.allfieldvaluesvalid()         ' handle case 1 or more field values invalid.         ' in case want redisplay form show error message listing invalid fields          model.hasattemptedupload = true         ' set tempdata before redirect:         tempdata("maxdocument") = model         return redirecttoaction("index")       else         ' field values valid, attempt add document     ...       end if      catch ex exception       system.diagnostics.debugger.break()     end try   'end if    ' if got far, failed, redisplay form   return view(model)  end function 

edit:

what appears happening browser has cached post action first post, , on every subsequent post (after first) renders cached results of first post instead of rendering results of current post. why doing this?

i have created class nocachecontrolattribute

nocachecontrolattribute.cs:

using system; using system.web; using system.web.mvc; namespace company.common.web.mvc {     public class nocachecontrolattribute : actionfilterattribute     {         private readonly httpcacheability _cacheability;         public nocachecontrolattribute(httpcacheability cacheability)         {             _cacheability = cacheability;         }         public override void onactionexecuted(actionexecutedcontext filtercontext)         {             httpcachepolicybase cache = filtercontext.httpcontext.response.cache;             cache.setallowresponseinbrowserhistory(false);             cache.setcacheability(_cacheability);             cache.setexpires(datetime.now);             cache.setnoservercaching();             cache.setnostore();         }     } } 

i cacheability parameter via autofac dependency injection / constructor injection.

...controller.cs

using ... using system.web; using system.web.mvc; using company.common.web.mvc; using ... namespace company.project.web.controllers {     [nocachecontrol(httpcacheability.nocache)]     public class contractcontroller : controller     {         private readonly irepository _repository;         public contractcontroller(irepository repository)         {             _repository = repository;         }         [httpget]         public actionresult actiontest(string id)         {             ...             return ...;         }     } } 

and so, caching disabled!


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -