ianodork
ianodork

Reputation: 27

Pass data between Views in MVC

I'm working on a Web Application project using C# and MVC that will take in two URLs in a form and use them to create an instance of a class I have created called "ImageSwap." This model has some data (a username of the person performing the swap; two variables which hold the URLs of two images to be swapped; two variables which save the actual names of these files without all of the rest of the URL information; and two arrays which represent the file locations to check for these files). Right now, I have it so that the initial index view creates the instance of the class and passes it to the same view with the information put in through the form, submitted via POST, like 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 and set needed values
        }
     }

It then performs some functions on the data, such as parsing out the filename at the end of the URL, and a directory number (which is the first part of this filename, i.e. directory#_fileName.jpg). All of this works fine.

My problem is that I would like to pass this model to another view once it has data populated in all of its fields by this initial ActionResult so that I can have a verification view, which would allow the user to preview the two files side by side so that they can ensure they are swapping the appropriate images. They should then be able to hit another submit button which will initiate the actual moving/replacing of the images and be taken to a page confirming.

Is there a way to pass data from this controller to a different view? My confusion arises because I cannot create another version of an ActionResult of Index with the same input, but I do not want to have the actual swapping of the images occur without a preview and a prompt. Should I re-write my Index view so that it utilizes partial views in order to accomplish this? What is the easiest way to have data persist through multiple steps and views?

Upvotes: 0

Views: 10519

Answers (2)

Joseph Woodward
Joseph Woodward

Reputation: 9281

What is the easiest way to have data persist through multiple steps and views?

Your question sounds like you're trying to achieve what you can easily do with sessions. The session object allows you to persist data between requests simply by adding it to the Session object on the HttpContext that exists within the base class that your controller extends, like so:

(Note the Serializable attribute. This allows your object to be serialized into the session object).

[Serializable]
public class ImageSwapModel {
    // Your class's properties
}

Then in your controller you can do the following:

[HttpPost]
public ActionResult Index(ImageSwapModel imageSwap)
{
    var oldFileFound = false;
    var newFileFound = false;

    if (ModelState.IsValid)
    {
        this.HttpContext.Session["ImageSwap"] = imageSwap;
    }
 }

When you want to retrieve the model you can grab it from the session like so:

var imageSwap = (ImageSwapModel)this.HttpContext.Session["ImageSwap"];

Taking it one step further:

Whilst the above will work fine, generally it's not a good practice to reference the HttpContext object directly in your code as it creates unnecessary coupling to the HttpContext object that can easily be avoided. Instead you should opt to inject an instance of the session object via Dependency Injection. Here is a similar answer that provides a basic idea as to how you can do this.

Upvotes: 3

Aram
Aram

Reputation: 5705

You can return different views with Models being passed to them in your one Index action like

if(some condition)
{
   Return View("ViewVersion1", MyModelVersion1);
}
else
{
   Return View("ViewVersion2", MyModelVersion2);
}

Upvotes: 1

Related Questions