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

  • Home
  • SEARCH
  • 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 9107251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T02:31:38+00:00 2026-06-17T02:31:38+00:00

Here is my view model: public class AgencyViewModel { public int VendorId { get;

  • 0

Here is my view model:

public class AgencyViewModel
{
    public int VendorId { get; set; }

    public IEnumerable<Vendor> Vendors { get; set; }

    public HttpPostedFileBase SpreadsheetFile { get; set; }
}

Here are my two controller methods:

public ActionResult Upload()
{
    var viewModel = new AgencyViewModel
    {
        Vendors = _vendorsRepository.Vendors.OrderBy(v => v.Name)
    };

    return View(viewModel);
}


[HttpPost]
public ActionResult Upload(AgencyViewModel viewModel)
{
    // do stuff
}

And my view:

@model MyModels.AgencyViewModel

@using (Html.BeginForm("Upload", "Agencies", FormMethod.Post,
    new { enctype = "multipart/form-data" }))
{
    @Html.DropDownListFor(model => model.VendorId,
        new SelectList(Model.Vendors, "VendorID", "Name"),
        "- Select a Vendor -")

    @Html.TextBoxFor(model => model.SpreadsheetFile, new { type = "file" })

    <p>
        <input type="submit" value="Next" />
    </p>
}

When I navigate to Upload, the Vendors are loaded correctly. But when I submit my form, my view model only contains VendorId and SpreadsheetFile, and Vendors is null.

I need to do some work with the Vendors collection after I submit the form, and I don’t want to load the all the data twice.

How can I keep the data in my view model?

  • 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-17T02:31:39+00:00Added an answer on June 17, 2026 at 2:31 am

    You have to set your ViewData again, you can use an ActionFilter if you want to keep it DRY, another choice would be overriding the controllers OnActionExecuted method and loading your data there.

    If you are doing good use of the MVC pattern you should only need to load the viewdata when the validation fails, since in that case you would load the same view again.

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

Sidebar

Related Questions

Here is my view model: [Serializable] public class MyViewModel { public int VendorId {
Here is my model: public class Items { public string Foo { get; set;
Model: public class Model { public ItemType Type { get; set; } public int
My View Model: public partial class FileTransferFilterCriteriaViewModel { public string Fice { get; set;
I have a collection of items. Here's my view model: public class MyViewModel {
What am I missing here? ViewModel: public class ViewModel { public IDictionary<int, string> Entities
I have a domain model: public class Project { [Key] public int ProjectID {
I have a fairly simple model: public class Delivery { public int DeliveryId {
I have the following entity model: public class Project { [Key] public int ProjectID
I have the following view model: public class ViewModel { //... [UIHint(Children) public IList<ChildViewModdel>

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.