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 6532477
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:00:28+00:00 2026-05-25T10:00:28+00:00

If I were to pass additional data (other than the model) into my view,

  • 0

If I were to pass additional data (other than the model) into my view, like say a list of files from a specific folder, whats the best way to do that?

I was thinking something like making a method and return a list into ViewData:

        public List<string> GetFiles(int id, string cat)
    {
        var files = new List<string>();

        var folder = "~/App_Data/uploads/" + cat + "/" + id.ToString();
        foreach (var file in Directory.GetFiles(folder))
        {
            files.Add(file);
        }
        return files;
    }

The controller:

ViewData["files"] = db.GetFiles(id, "random");

The view:

@foreach (var item in ViewData["files"]){ //markup }

First, ive heard that viewdata and viewbag shouldnt be used. Second, this code doesnt work. Something with Viewdata object is not enumerable. Should I make a model class for these files. If so, how? 2 models in one view? Im a bit confused how to do this proberly.

  • 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-05-25T10:00:29+00:00Added an answer on May 25, 2026 at 10:00 am

    If I were to pass additional data (other than the model) into my view,
    like say a list of files from a specific folder, whats the best way to
    do that?

    Write a view model which will contain all the properties your view needs and have your controller action pass this view model to the view. So:

    public class MyViewModel
    {
        public List<string> Files { get; set; }
    
        public string Foo { get; set; }
    
        ... some other properties that your view might need
    }
    

    then the controller:

    public ActionResult Index(int id)
    {
        var model = new MyViewModel
        {
            Files = db.GetFiles(id, "random"),
            Foo = "bar"
        };
        return View(model);
    }
    

    and finally in your strongly typed view:

    @model MyViewModel
    @foreach (var file in Model.Files)
    {
        <div>@file</div>
    }
    
    <div>@Html.DisplayFor(x => x.Foo)</div>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

how do I pass additional information to the service method returning the collection of
I am trying, in vain it seems, to be able to pass additional parameters
I would like to pass an argument(s) to a method being defined using define_method,
I am trying to pass a string over to a detail view of my
I have this logical problem regarding on how to pass value from one JFrame
I would like to generate various data types in C++ with unique deterministic names.
I'm using Java to transform xml files using the Transformer class. I pass a
How do I pass parameters from a HTML file to a external Javascript file
I pass in the following XML to XMLin : <root foo=bar foo2=bar2> <pizzas> <pizza>Pepperoni</pizza>
Goal Pass images generated by one process efficiently and at very high speed to

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.