Using IFormFile in form with fields from other model in razor page
I am beginner in Asp.Net core 2.1 razor page, and I am facing a trouble trying to use IFormFile in a form with fields from other model - I am using razor pages.
In the Post method, the IFormFile object come null. On the page, after select a file, looks like the object was filled up, but in the controller, his value was lost.
Below are the models I am using:
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; }
}
}
My page:
@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");}
}
And the Controller:
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);
}
}
}
Please, could anybody help me.
Thanks
iformfile
add a comment |
I am beginner in Asp.Net core 2.1 razor page, and I am facing a trouble trying to use IFormFile in a form with fields from other model - I am using razor pages.
In the Post method, the IFormFile object come null. On the page, after select a file, looks like the object was filled up, but in the controller, his value was lost.
Below are the models I am using:
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; }
}
}
My page:
@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");}
}
And the Controller:
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);
}
}
}
Please, could anybody help me.
Thanks
iformfile
add a comment |
I am beginner in Asp.Net core 2.1 razor page, and I am facing a trouble trying to use IFormFile in a form with fields from other model - I am using razor pages.
In the Post method, the IFormFile object come null. On the page, after select a file, looks like the object was filled up, but in the controller, his value was lost.
Below are the models I am using:
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; }
}
}
My page:
@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");}
}
And the Controller:
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);
}
}
}
Please, could anybody help me.
Thanks
iformfile
I am beginner in Asp.Net core 2.1 razor page, and I am facing a trouble trying to use IFormFile in a form with fields from other model - I am using razor pages.
In the Post method, the IFormFile object come null. On the page, after select a file, looks like the object was filled up, but in the controller, his value was lost.
Below are the models I am using:
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; }
}
}
My page:
@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");}
}
And the Controller:
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);
}
}
}
Please, could anybody help me.
Thanks
iformfile
iformfile
asked Nov 21 '18 at 16:59
Evandro RamalhoEvandro Ramalho
11
11
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53417131%2fusing-iformfile-in-form-with-fields-from-other-model-in-razor-page%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53417131%2fusing-iformfile-in-form-with-fields-from-other-model-in-razor-page%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown