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

The Archive Base Latest Questions

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

I have an ActionResult which binds data to the model and adds it the

  • 0

I have an ActionResult which binds data to the model and adds it the the DB. Now what I want, is to have a file uploader, and the ActionResult to store the files and adding their FileName to the DB (so I can show the files/images at a later point). What is the best approach? Here is what I got so far (It can store the files, but im not sure how intelligent EF is, and how complex the datatypes can be):

The model class:

    public class Annonce
{
    public int Id { get; set; }
    public string Company { get; set; }
    public string Size { get; set; }
    public IEnumerable<HttpPostedFileBase> FileUpload { get; set; }
}

The view (using mircosoft.web.helpers):

    @using (Html.BeginForm("Create", "Annonce", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Annonce</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.Company)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Company)
            @Html.ValidationMessageFor(model => model.Company)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Size)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Size)
            @Html.ValidationMessageFor(model => model.Size)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.FileUpload)
        </div>
        <div class="editor-field">
 @FileUpload.GetHtml(uploadText: "Opret")
        </div>
    </fieldset>

The controller:

[HttpPost]
    public ActionResult Create(Annonce annonce)
    {
        if (ModelState.IsValid)
        {                
            //System.IO stuff
            foreach (var file in annonce.FileUpload)
            {
                if (file.ContentLength > 0)
                {
                    var fileName = Path.GetFileName(file.FileName);
                    var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
                    file.SaveAs(path);

                }
            }
            //Database stuff
            db.Annoncer.Add(annonce);
            db.SaveChanges();
            return RedirectToAction("Index");  
        }

        return View(annonce);
    }

This stores the files just fine. Now what I want, is file.FileName to be stores in the db. At first, I though EF would simply bind the files or filenames from model.FileUpload to the db. But I guess the datatype is too complex? So I though of making a list<HttpPostedFileBase> and adding the file.FileName there? Or maybe create an entire new entity/table where all the filenames are stored as strings with a reference-Id?
Any suggestions?

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

    I don’t think you want to save the actual file contents in the database.
    So I would do something like:

    public class AnnonceFile
    {
        public string filename {get;set;}
    }
    
    public class Annonce
    {
        public int Id { get; set; }
        public string Company { get; set; }
        public string Size { get; set; }
        public ICollection<AnnonceFile> Filenames{ get; set; }
    }
    
    
            //System.IO stuff
            foreach (var file in files)
            {
                if (file.ContentLength > 0)
                {
                    var fileName = Path.GetFileName(file.FileName);
                    var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
                    file.SaveAs(path);
                    annonce.Filenames.Add( new AnnonceFile {filename = path});
    
                }
            }
            //Database stuff
            db.Annoncer.Add(annonce);
            db.SaveChanges();
            return RedirectToAction("Index");  
    
    1. You need to adjust the path saved to afterwards, since asp.net won’t allow you to serve files from the App_Data folder.
    2. the public ICollection<string> Filenames is just to give you an idea, but you probably need to change that in ICollection<FileEntity> Files.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using ASP.NET MVC 1.0. I have an ActionResult which receives a form
I have the following code: public ActionResult SomeAction() { return new JsonpResult { Data
I have a controller action which renders a partial view: public ActionResult Details(int id)
I have a custom model class which contains a decimal member and a view
I have a view which displays a model eg: public class MyModel() { public
I have a complex view model which contains a collection of other objects which
I have a strong typed view model and a MetaData partial class which has
I have a view which accepts the following model: Inherits=System.Web.Mvc.ViewPage<MVC_WordsByME.Models.JobCreationModel> This posts back to
I have a site which is using facebook for auth. I want to gather
I want to simply validate a single property of that model public ActionResult Rate([Bind(Exclude=Score)]RatingModel

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.