在表单中将IFormFile与剃刀页面中其他模型的字段一起使用

时间:2018-11-21 16:59:57

标签: 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);
        }
    }
}

请,有人可以帮我吗?

谢谢

0 个答案:

没有答案