Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8027325
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T23:46:33+00:00 2026-06-04T23:46:33+00:00

As a fairly experienced ASP.Net developer just recently starting using MVC, I find myself

  • 0

As a fairly experienced ASP.Net developer just recently starting using MVC, I find myself struggling a bit to change my mindset from a traditional “server control and event handler” way of doing things, into the more dynamic MVC way of things. I think I am slowly getting there, but sometimes the MVC “magic” throws me off.

My current scenario is to create a web page that allows the user to browse a local file, upload it to the server and repeat this until he has a list of files to work with. When he is happy with the file list (which will be displayed in a grid on the page), he will click a button to process the files and extract some data that will be stored in a database.

The last part is not so important, right now I am struggling with something as trivial as building up a list of files, and persisting that list between requests. In the traditional approach this would be extremely simple – the data would be persisted in ViewState. But in MVC I need to pass the data between the controller and the views, and I don’t fully get how this is supposed to work.

I guess I better post my rather incomplete attempt of coding this to explain the problem.

In order to keep my file list data, I have created a viewmodel that is basically a typed list of files, along with some extra metadata:

public class ImportDataViewModel
{
    public ImportDataViewModel()
    {
        Files = new List<ImportDataFile>();
    }

    public List<ImportDataFile> Files { get; set; }
...

In the view, I have a form for browsing and uploading the file:

<form action="AddImportFile" method="post" enctype="multipart/form-data">
  <label for="file">
    Filename:</label>
  <input type="file" name="file" id="file" />
  <input type="submit" />
  </form>

The view is using the viewmodel as its model:

@model MHP.ViewModels.ImportDataViewModel

This will send the file to my action:

public ActionResult AddImportFile(HttpPostedFileBase file, ImportDataViewModel importData)
    {

        if (file.ContentLength > 0)
        {
            ImportDataFile idFile = new ImportDataFile { File = file };
            importData.Files.Add(idFile);
        }

        return View("DataImport", importData);
    }

This action returns the view for the DataImport page along with the viewmodel instance containing the list of files.

This works nicely up to a certain point, I can browse a file and upload it, and I can see the viewmodel data inside the action, and then also if I put a breakpoint inside the view and debug “this.Model”, everything is fine.

But then, if I try to upload another file, when putting a breakpoint inside the AddImportFile action, the importData parameter is empty. So the view is obviously not passing the current instance of its model to the action.

In the MVC samples I have been through, the model instance is “magically” passed to the action method as a parameter, so why is it empty now?

I assume the real problem is my limited understanding of MVC, and that there is probably a very simple solution to this. Anyways, I would be extremely grateful if somebody could point me in the right direction.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T23:46:34+00:00Added an answer on June 4, 2026 at 11:46 pm

    It’s been some time since I posted the question, which was quite colored by my little experience and knowledge of MVC. Still I received some quite useful input, that eventually led me to find a solution and also gain some insight of MVC.

    What threw me off in the first place, was that you could have a controller with a strongly typed object as a parameter, like this:

    public ActionResult DoSomething(MyClass myObject)...
    

    This object originated from the same controller:

    ...
    return View(myObject);
    ...
    

    This lead me to believe that the object lived throughout these two steps, and that I somehow could expect that you could send it to the view, do something and then “magically” get it back to the controller again.

    After reading up about model binding, I understood that this is of course not the case. The view is completely dead and static, and unless you store the information somewhere, it is gone.

    Going back to the problem, which was selecting and uploading files from the client, and building up a list of these files to be displayed, I realized that in general there are three ways to store information between requests in MVC:

    1. You can store information in form fields in the view, and post it back to the controller later
    2. You can persist it in some kind of storage, e.g. a file or a database
    3. You can store it in server memory by acessing objects that lives throughout requests, e.g. session variables

    In my case, I had basically two types of information to persist:
    1. The file metadata (file name, file size etc.)
    2. The file content

    The “by-the-book” approach would probably be to store the metadata in form fields, and the file contents in a file or in db. But there is also another way. Since I know my files are quite small and there will be only a few of them, and this solution will never be deployed in a server farm or similar, I wanted to explore the #3 option of session variables. The files are also not interesting to persist beyond the session – they are processed and discarded, so I did not want to store them in my db.

    After reading this excellent article:
    Accessing ASP.NET Session Data Using Dynamics

    I was convinced. I simply created a sessionbag class as described in the article, and then I could do the following in my controller:

        [HttpPost]
        public ActionResult AddImportFile(HttpPostedFileBase file)
        {
    
            ImportDataViewModel importData = SessionBag.Current.ImportData;
            if (importData == null) importData = new ImportDataViewModel();
    
            if (file == null)
                return RedirectToAction("DataImport");
    
            if (file.ContentLength > 0)
            {
                ImportDataFile idFile = new ImportDataFile { File = file };
                importData.Files.Add(idFile);
            }
    
            SessionBag.Current.ImportData = importData;
    
            return RedirectToAction("DataImport");
        }
    

    I am fully aware that in most cases, this would be a bad solution. But for the few kb of server memory the files occupy and with the simplicity of it all, I think it worked out very nicely for me.

    The additional bonus of using the SessionBag is that if the user entered a different menu item and then came back, the list of files would still be there. This would not be the case e.g. when choosing the form fields/file storage option.

    As a final remark, I realize that the SessionBag is very easy to abuse, given the simplicity of use. But if you use it for what it is meant for, namely session data, I think it can be a powerful tool.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am just starting to investigate ASP.NET MVC, and I am using the latest
HI, I am a fairly experienced ASP developer making the move to ASP.net. I
I'm a fairly experienced .NET desktop developer, but I'm new to Silverlight. I'm trying
I'm a fairly experienced developer (.NET, C#, XML, Webservices, HTML, Javascript, various databases etc.)
I am a fairly experienced (mid-level) developer that has spent quite a bit of
I'm a fairly experienced programmer and I've just recently discovered node.js. I love JavaScript
I'm working on an ASP.NET MVC site, using EF4 and Automapper. I'm making good
I have had good experiences recently developing with ASP.NET MVC and am considering whether
Is it possible for a fairly experienced Java Developer to transition to being a
To learn ASP.net I started building a todo web app(web forms not MVC), this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.