1

I am new to ASP.Net, if my question sounds very basic, please be polite.

I have created a ASP.Net MVC4 project. A page in the application will display names of the files in tabular form.

The names of the files are obtained by finding files in a particular folder on the server. So there is no need for me to have a database for model.

When user opens the page, the server side code will list all the files in the directory and should return a list of Strings.

I am following ASP.Net MVC tutorial here and it appears I have to have a DBContext class and also a database. In my case that's not needed

So that question is can I add my model without having to add code like below and still be able to use the feature of model ? any simple example would be great.

 public class MovieDBContext : DbContext 
 {
  public DbSet<Movie> Movies { get; set; } 
 }

4 Answers 4

3

Any class can serve as a model in this case. For example, if you just want to have a Movie model, you can have something as simple as this:

public class Movie
{
    public string Title { get; set; }
}

A view can bind to that model, to an enumeration of that model, etc.

For fetching your data from the file system, one approach could be a simple factory method on that model. Maybe something like this:

public class Movie
{
    public string Title { get; set; }

    public static IEnumerable<Movie> GetMovies()
    {
        // get the list of movies from the file system, for example as a list of strings
        var movies = SomeFileSystemInteraction();
        return movies.Select(m => new Movie { Title = m });
    }
}

Then in your controller you can get the list of movies to bind to the view:

public ActionResult Index()
{
    return View(Movie.GetMovies());
}

There's no need for a database, it's just used in tutorials because it's the most common case. But you can bind your views to any object you'd like.

Sign up to request clarification or add additional context in comments.

Comments

1

You do not need that MovieDBContext class if you are not dealing with database. But having a model is a good idea to represent your entity(in this case the Folder). It is just a POCO class.

public class Folder
{
  public string Name { set;get;}
  public int NumberOfchilds { set;get;}
  //Other properties as needed.
}

Now you can use the Model class to pass data between differnt places. For example. You can move the code which reads the data from file system to a Service class method and the method can return a list of this class/A single instance of this class as needed. Your controller action method can call the service method.

public class FolderService
{
  public Folder GetRecentlyCreatedFolder()
  { 
     var folder=new Folder();
     //Set the properties of this object
     // folder.Name="MySecret";
     return folder; 
  }
}

and in your Action method

public ActionResult Items()
{
  var srv=new FolderService();
  var recentFolder=srv.GetRecentlyCreatedFolder();
  return View(recentFolder);
}

Comments

0

Yes, you can use a model which is not stored in a database at all. Your case sounds like an obvious example.

Model, View, Controller makes database access easily abstractable but doesn't necessitate a database.

Comments

0

As much as I like entity framework I've never loved how most tutorials for MVC seem to marry the two - They are essentially unrelated and I think it really muddies the waters for a lot of programmers new to the framework.

All you need for a model is just a plain old class. Instead of using the dbcontext you'd simply use whatever code you need to load your data.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.