Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Razor ASP核心剃须刀-如何将文件上载到SQL数据库?_Razor_Asp.net Core_File Upload_Entity Framework Core - Fatal编程技术网

Razor ASP核心剃须刀-如何将文件上载到SQL数据库?

Razor ASP核心剃须刀-如何将文件上载到SQL数据库?,razor,asp.net-core,file-upload,entity-framework-core,Razor,Asp.net Core,File Upload,Entity Framework Core,很久以前,我使用ASP.NET为工作创建了一个应用程序。我现在正在使用Razor页面(ASP.NET核心)重写应用程序。如果问题太简单,或者其他地方已经回答了,我表示歉意。我找了好几天了!我无法将文件上载到表中 模型如下所示: using System; using System.Collections.Generic; namespace FinplianceProject.Models { public partial class TblSdgs { pub

很久以前,我使用ASP.NET为工作创建了一个应用程序。我现在正在使用Razor页面(ASP.NET核心)重写应用程序。如果问题太简单,或者其他地方已经回答了,我表示歉意。我找了好几天了!我无法将文件上载到表中

模型如下所示:

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; }
    }
}
@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");}
}
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);
        }
    }
}
我的剃须刀页面如下所示:

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; }
    }
}
@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");}
}
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);
        }
    }
}
@page
@模型FinComplianceProject.Pages.SDGs.EditModel
@{
ViewData[“标题”]=“编辑”;
}
编辑
TblSdgs

返回列表 @节脚本{ @{wait Html.RenderPartialAsync(“_validationScript”);} }
后端如下所示:

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; }
    }
}
@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");}
}
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);
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Threading.Tasks;
使用Microsoft.AspNetCore.Mvc;
使用Microsoft.AspNetCore.Mvc.RazorPages;
使用Microsoft.AspNetCore.Mvc.Rendering;
使用Microsoft.EntityFrameworkCore;
使用FinComplianceProject.Models;
命名空间FinplianceProject.Pages.SDGs
{
公共类EditModel:PageModel
{
私有只读FinComplianceProject.Models.DB_19456_ccsolutions1Context_context;
公共编辑模型(FinComplianceProject.Models.DB_19456_ccsolutions1Context上下文)
{
_上下文=上下文;
}
[BindProperty]
公共TblSdgs TblSdgs{get;set;}
公共异步任务OnGetAsync(int?id)
{
if(id==null)
{
返回NotFound();
}
TblSdgs=await _context.TblSdgs.FirstOrDefaultAsync(m=>m.IntSdgId==id);
如果(TblSdgs==null)
{
返回NotFound();
}
返回页();
}
公共异步任务OnPostAsync()
{
如果(!ModelState.IsValid)
{
返回页();
}
_Attach(TblSdgs).State=EntityState.Modified;
尝试
{
wait_context.SaveChangesAsync();
}
catch(DbUpdateConcurrencyException)
{
if(!TblSdgsExists(TblSdgs.IntSdgId))
{
返回NotFound();
}
其他的
{
投掷;
}
}
返回页首(“/索引”);
}
私人bool-tblsdg性别歧视者(int id)
{
返回_context.TblSdgs.Any(e=>e.IntSdgId==id);
}
}
}
页面显示OK,文件上传控件正常工作,但当我单击“保存”时,文件不会保存


请提前帮助和感谢

欢迎来到SO!当您单步执行POST方法时,
ImgSdgIconNormal
imgsdgiconverted
是否为null?是的,它们为null此答案详细介绍了如何使用
ifformfile
viewmodel属性,以及如何将字节数组保存到数据库:谢谢您,查克达。razor页面中的视图模型在哪里?我看到了页面模型,但没有看到视图模型。另外,GET操作方法在哪里?谢谢。查看以下教程:欢迎来到SO!当您单步执行POST方法时,
ImgSdgIconNormal
imgsdgiconverted
是否为null?是的,它们为null此答案详细介绍了如何使用
ifformfile
viewmodel属性,以及如何将字节数组保存到数据库:谢谢您,查克达。razor页面中的视图模型在哪里?我看到了页面模型,但没有看到视图模型。另外,GET操作方法在哪里?谢谢。请查看以下教程: