1

I created an app for work a long time ago using ASP.NET. I am now rewriting the app using Razor pages (ASP.NET core). I apologize if the question is too simple or if it has been answered elsewhere. I've searched for days! I'm having difficulty uploading a file to a table.

The model looks like this:

using System;
using System.Collections.Generic;

namespace FinplianceProject.Models
{
    public partial class TblSdgs
    {
        public int IntSdgId { get; set; }
        public string StrSdgShort { get; set; }
        public string StrSdgLong { get; set; }
        public byte[] ImgSdgIconNormal { get; set; }
        public byte[] ImgSdgIconInverted { get; set; }
    }
}

My razor page looks like this:

@page
@model FinplianceProject.Pages.SDGs.EditModel

@{
    ViewData["Title"] = "Edit";
}

<h2>Edit</h2>

<h4>TblSdgs</h4>
<hr />
<div class="row">
    <div class="col-md-4">
        <form method="post"  enctype="multipart/form-data" asp-controller="UploadFiles" asp-action="Index">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <input type="hidden" asp-for="TblSdgs.IntSdgId" />
            <div class="form-group">
                <label asp-for="TblSdgs.StrSdgShort" class="control-label"></label>
                <input asp-for="TblSdgs.StrSdgShort" class="form-control" />
                <span asp-validation-for="TblSdgs.StrSdgShort" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="TblSdgs.StrSdgLong" class="control-label"></label>
                <input asp-for="TblSdgs.StrSdgLong" class="form-control" />
                <span asp-validation-for="TblSdgs.StrSdgLong" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="TblSdgs.ImgSdgIconNormal" class="control-label"></label>
                <input type="file" asp-for="TblSdgs.ImgSdgIconNormal" />
            </div>
            <div class="form-group">
                <label asp-for="TblSdgs.ImgSdgIconInverted" class="control-label"></label>
                <input type="file" asp-for="TblSdgs.ImgSdgIconInverted" />
            </div>
            <div class="form-group">
                <input type="submit" value="Save" class="btn btn-default" />
            </div>
        </form>
    </div>
</div>

<div>
    <a asp-page="./Index">Back to List</a>
</div>

@section Scripts {
    @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}

The backend looks like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using FinplianceProject.Models;

namespace FinplianceProject.Pages.SDGs
{
    public class EditModel : PageModel
    {
        private readonly FinplianceProject.Models.DB_19456_ccsolutions1Context _context;

        public EditModel(FinplianceProject.Models.DB_19456_ccsolutions1Context context)
        {
            _context = context;
        }

        [BindProperty]
        public TblSdgs TblSdgs { get; set; }

        public async Task<IActionResult> OnGetAsync(int? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            TblSdgs = await _context.TblSdgs.FirstOrDefaultAsync(m => m.IntSdgId == id);

            if (TblSdgs == null)
            {
                return NotFound();
            }
            return Page();
        }

        public async Task<IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return Page();
            }

            _context.Attach(TblSdgs).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!TblSdgsExists(TblSdgs.IntSdgId))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return RedirectToPage("./Index");
        }

        private bool TblSdgsExists(int id)
        {
            return _context.TblSdgs.Any(e => e.IntSdgId == id);
        }
    }
}

The page displays OK with a file upload control that works, but when I click "Save", the files are not saved.

Please help and thanks in advance!

chakeda
  • 1,551
  • 1
  • 18
  • 40
  • Welcome to SO! Is `ImgSdgIconNormal` and `ImgSdgIconInverted` null when you step through the POST method? – chakeda Mar 01 '19 at 22:22
  • Yes, they are null – Pierre Sogol Mar 01 '19 at 22:23
  • This answer details how to use the `IFormFile` viewmodel property, and how to save the byte array to the database: https://stackoverflow.com/questions/35379309/how-to-upload-files-in-asp-net-core – chakeda Mar 01 '19 at 22:46
  • Thank you, chakeda. Wherei s the view model in the razor page? I see the page model, but not the view model. Also, where is the GET action method? Thanks. – Pierre Sogol Mar 02 '19 at 01:03
  • Check the following tutorial : [Upload files to a Razor Page in ASP.NET Core](https://learn.microsoft.com/en-us/aspnet/core/razor-pages/upload-files?view=aspnetcore-2.2) – Nan Yu Mar 04 '19 at 09:15

0 Answers0