Использование IFormFile в форме с полями из другой модели на странице бритвы
Я новичок в странице бритвы Asp.Net Core 2.1, и я столкнулся с проблемой, пытаясь использовать IFormFile в форме с полями из другой модели - я использую страницы бритвы.
В методе Post объект IFormFile имеет значение null. На странице, после выбора файла, выглядит, как объект был заполнен, но в контроллере его значение было потеряно.
Ниже приведены модели, которые я использую:
using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace PSD.Models
{
public class Contractor
{
public int ContractorID { get; set; }
public string RegisteredNumber { get; set; }
public string Name { get; set; }
public string Alias { get; set; }
public string LogoBytes { get; set; }
}
}
using Microsoft.AspNetCore.Http;
using System.ComponentModel.DataAnnotations;
namespace PSD.Models
{
public class FileUpload
{
public string FileName { get; set; }
public IFormFile UploadFile { get; set; }
}
}
Моя страница:
@page "{id:int?}"
@model PSD.Pages.Contractors.EditModel
@{
ViewData["Title"] = "Contratante";
}
<div class="m-portlet__head">
<div class="m-portlet__head-caption">
<div class="m-portlet__head-title">
<span class="m-portlet__head-icon m--hide">
<i class="la la-gear"></i>
</span>
<h3 class="m-portlet__head-text">
Cadastro de Contratante - Alteração
</h3>
</div>
</div>
</div>
<form method="post" class="m-form" enctype="multipart/form-data">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<input type="hidden" asp-for="Contractor.ContractorID" />
<div class="m-portlet__body">
<div class="m-form__section m-form__section--first">
<div class="form-group m-form__group">
<label asp-for="Contractor.RegisteredNumber" class="control-label"></label>
<input asp-for="Contractor.RegisteredNumber" class="form-control" />
<span asp-validation-for="Contractor.RegisteredNumber" class="text-danger"></span>
</div>
<div class="form-group m-form__group">
<label asp-for="Contractor.Name" class="control-label"></label>
<input asp-for="Contractor.Name" class="form-control" />
<span asp-validation-for="Contractor.Name" class="text-danger"></span>
</div>
<div class="form-group m-form__group">
<label asp-for="Contractor.Alias" class="control-label"></label>
<input asp-for="Contractor.Alias" class="form-control" />
<span asp-validation-for="Contractor.Alias" class="text-danger"></span>
</div>
<div class="form-group m-form__group">
<label asp-for="Contractor.LogoBytes" class="control-label"></label>
</div>
<label for="file" class="btn btn-success m-btn m-btn--icon"><i class="la la-cloud-upload"></i> Upload</label>*@
<input asp-for="FileUpload.UploadFile" type="file" class="form-control" style="height:auto" />
</div>
</div>
<div class="m-portlet__foot m-portlet__foot--fit">
<div class="m-form__actions m-form__actions">
<input type="submit" name="answer" value="Gravar" class="btn btn-primary" />
<a asp-page="./Index" class="btn btn-secondary">
<span>
Cancelar
</span>
</a>
</div>
</div>
</form>
@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 PSD.Data;
using PSD.Models;
using PSD.Utilities;
namespace PSD.Pages.Contractors
{
public class EditModel : PageModel
{
private readonly PSD.Data.PSDContext _context;
public EditModel(PSD.Data.PSDContext context)
{
_context = context;
}
[BindProperty]
public FileUpload FileUpload { get; set; }
[BindProperty]
public Contractor Contractor { get; set; }
public async Task<IActionResult> OnGetAsync(int? id)
{
if (id == null)
{
return NotFound();
}
Contractor = await _context.Contractor.FirstOrDefaultAsync(m => m.ContractorID == id);
if (Contractor == null)
{
return NotFound();
}
return Page();
}
public async Task<IActionResult> OnPostAsync(string answer)
{
if (!ModelState.IsValid || string.IsNullOrWhiteSpace(answer))
{
return Page();
}
_context.Attach(Contractor).State = EntityState.Modified;
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!ContractorExists(Contractor.ContractorID))
{
return NotFound();
}
else
{
throw;
}
}
return RedirectToPage("./Index");
}
private bool ContractorExists(int id)
{
return _context.Contractor.Any(e => e.ContractorID == id);
}
}
}
Пожалуйста, кто-нибудь может мне помочь.
Спасибо