1

I have the code working in a new seperate test project how ever it will not work with my excisting work project. I am trying to upload a file as part of a form, store it in a folder in wwwroot and store the url in a database. I have posted the code below.The program compiles and runs but does not store anything when create button is pressed. any help would be greatly appreciated.

//Model

namespace PostProjectEvaluations.Web.Models
{
public partial class Projects
{
    [Key]
    public int ProjectId { get; set; }
    [Required]
    [StringLength(300)]
    public string Name { get; set; }
    [Required]
    [StringLength(50)]
    public string Manager { get; set; }

    public string FilePath { get; set; }

}
public class ProjectsVM
{
    public string Name { get; set; }
    public IFormFile File { get; set; }
}

//Controller

namespace PostProjectEvaluations.Web.Controllers
{
    public class projectsController : Controller
    {
        private readonly IApplicationRepository ApplicationRepository;
        private readonly PostProjectEvaluationsContext _context;
        private IHostingEnvironment mxHostingEnvironment { get; set; }
        private object objproject;

        public projectsController(IApplicationRepository applicationRepository,
            IHostingEnvironment hostingEnvironment, PostProjectEvaluationsContext context)
        {
            mxHostingEnvironment = hostingEnvironment;
            ApplicationRepository = applicationRepository;
            _context = context;
        }


        public IActionResult Index()
        {
            ViewBag.dataSource = ApplicationRepository.GetAllProjects().ToList();
            var projects = ApplicationRepository.GetAllProjects();
            return View(projects);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Index(ProjectsVM projectsVM)
        {
            if (projectsVM.File != null)
            {
                //upload files to wwwroot
                var fileName = Path.GetFileName(projectsVM.File.FileName);
                var filePath = Path.Combine(mxHostingEnvironment.WebRootPath, "Uploads", fileName);


                using (var fileSteam = new FileStream(filePath, FileMode.Create))
                {
                    await projectsVM.File.CopyToAsync(fileSteam);
                }
                //your logic to save filePath to database, for example

                Projects projects = new Projects();
                projects.Name = projectsVM.Name;
                projects.FilePath = filePath;

                _context.Projects.Add(projects);
                _context.SaveChanges();
            }
            else
            {

            }
            return View("Index");
        }
  public IActionResult Details(int id)
        {
            var project = ApplicationRepository.GetProjects(id);
            return View(project);
        }

        [HttpGet]
        public IActionResult Create()
        {

            var project = new Projects();
            return View(project);
        }

        [HttpPost]
        public IActionResult Create(Projects projects)
        {
            ApplicationRepository.Create(projects);
            return RedirectToAction("Index");

        }



        public IActionResult Delete(int id)
        {
            var project = ApplicationRepository.GetProjects(id);
            ApplicationRepository.Delete(project);
            return RedirectToAction("Index");
        }


        [HttpGet]
        public IActionResult Edit(int id)
        {
            var project = ApplicationRepository.GetProjects(id);
            //mxApplicationRepository.SaveChangesAsync();
            return View(project);
        }
        [HttpPost]
        public IActionResult Edit(Projects projects)
        {
            ApplicationRepository.Edit(projects);
            //mxApplicationRepository.SaveChangesAsync();
            return RedirectToAction("Index");
        }


    }

}

//View

  <form enctype="multipart/form-data" asp-controller="Projects" asp-action="Create" method="post" class="form-horizontal">
        <div asp-validation-summary="ModelOnly" class="text-danger"></div>
        <div class="form-body">
            <h3 class="form-section">Project Info</h3>
            <div class="row">
                <div class="col-md-6">
                    <div class="form-group">
                        <label class="control-label col-md-3">Project Name</label>
                        <div class="col-md-9">
                            <input asp-for="Name" class="form-control" placeholder="Name">
                            <span asp-validation-for="Name" class="text-danger"></span>
                        </div>
                    </div>
                </div>
                <!--/span-->
                <div class="col-md-6">
                    <div class="form-group">
                        <label class="control-label col-md-3">Project Manager</label>
                        <div class="col-md-9">
                            <input asp-for="Manager" class="form-control" placeholder="Name">
                        </div>
                    </div>
                </div>
                <!--/span-->
            </div>

            <!--/span-->
        </div>
        <h3 class="form-section">Project Files</h3>
        <!--/row-->
        <div class="row">
            <div>
                <div class="col-md-6">
                    <div class="col-md-10">
                        <p>Upload one or more files using this form:</p>
                        <input type="file" name="files" multiple />
                    </div>
                </div>
            </div>
            <div>
            </div>
            </div>

            <div class="form-actions right">
                <input type="submit" class="btn blue-assembly" name="submitButton" value="Save" />
                <a asp-action="Index" class="btn default" onclick="cancelClick()">Cancel</a>
            </div>

3
  • Can you check if projectsVM has a value? i don't think that the model binder is able to bind your input field to this variable because the name on the input field is "files". You could try something like List<IFormFile> files as your parameter in the save function Commented Mar 27, 2019 at 12:25
  • Double check that your file is not null, and I think you'll have to replace WebRootPath with ContentRootPath. also check your Debug folder from where your application is ran maybe it's saving the file there. Commented Mar 27, 2019 at 12:35
  • In debug output i get this error, Cannot insert the value NULL into column 'FilePath', table 'PostProjectEvaluations.dbo.Projects'; column does not allow nulls. INSERT fails. Commented Mar 27, 2019 at 13:43

1 Answer 1

1

Include your model in view as in the first line

@ProjectsVM

You have to give the same name as a model Try this:

   <input type="file" id= name="File" multiple />
Sign up to request clarification or add additional context in comments.

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.