c# - Pass data between Views in MVC -


i'm working on web application project using c# , mvc take in 2 urls in form , use them create instance of class have created called "imageswap." model has data (a username of person performing swap; 2 variables hold urls of 2 images swapped; 2 variables save actual names of these files without of rest of url information; , 2 arrays represent file locations check these files). right now, have initial index view creates instance of class , passes same view information put in through form, submitted via post, so:

public actionresult index() { ops.operations.models.imageswapmodel newimageswap = new models.imageswapmodel(); return view(newimageswap); }  [httppost]     public actionresult index(imageswapmodel imageswap)     {         var oldfilefound = false;         var newfilefound = false;          if (modelstate.isvalid)         {         //perform data manipulation , set needed values         }      } 

it performs functions on data, such parsing out filename @ end of url, , directory number (which first part of filename, i.e. directory#_filename.jpg). of works fine.

my problem pass model view once has data populated in of fields initial actionresult can have verification view, allow user preview 2 files side side can ensure swapping appropriate images. should able hit submit button initiate actual moving/replacing of images , taken page confirming.

is there way pass data controller different view? confusion arises because cannot create version of actionresult of index same input, not want have actual swapping of images occur without preview , prompt. should re-write index view utilizes partial views in order accomplish this? easiest way have data persist through multiple steps , views?

what easiest way have data persist through multiple steps , views?

your question sounds you're trying achieve can sessions. session object allows persist data between requests adding session object on httpcontext exists within base class controller extends, so:

(note serializable attribute. allows object serialized session object).

[serializable] public class imageswapmodel {     // class's properties } 

then in controller can following:

[httppost] public actionresult index(imageswapmodel imageswap) {     var oldfilefound = false;     var newfilefound = false;      if (modelstate.isvalid)     {         this.httpcontext.session["imageswap"] = imageswap;     }  } 

when want retrieve model can grab session so:

var imageswap = (imageswapmodel)this.httpcontext.session["imageswap"]; 

taking 1 step further:

whilst above work fine, it's not practice reference httpcontext object directly in code creates unnecessary coupling httpcontext object can avoided. instead should opt inject instance of session object via dependency injection. here similar answer provides basic idea how can this.


Comments

Popular posts from this blog

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - UML - How would you draw a try catch in a sequence diagram? -

c++ - No viable overloaded operator for references a map -