Before code generated:
@page @model pcore31.CreateModel @{ Layout = null; } <!DOCTYPE html><html><head><meta name="viewport" content="width=device-width" /><title>Create</title></head><body><h4>Pet</h4><hr /><div class="row"><div class="col-md-4"><form method="post"><div asp-validation-summary="ModelOnly" class="text-danger"></div><div class="form-group"><label asp-for="Pet.PetName" class="control-label"></label><input asp-for="Pet.PetName" class="form-control" /><span asp-validation-for="Pet.PetName" class="text-danger"></span></div><div class="form-group"><label asp-for="Pet.Dogpic" class="control-label"></label><input asp-for="Pet.Dogpic" class="form-control" /><span asp-validation-for="Pet.Dogpic" class="text-danger"></span></div><div class="form-group"><label asp-for="Pet.Odate" class="control-label"></label><input asp-for="Pet.Odate" class="form-control" /><span asp-validation-for="Pet.Odate" class="text-danger"></span></div><div class="form-group form-check"><label class="form-check-label"><input class="form-check-input" asp-for="Pet.Ocheck" /> @Html.DisplayNameFor(model => model.Pet.Ocheck)</label></div><div class="form-group"><input type="submit" value="Create" class="btn btn-primary" /></div></form></div></div><div><a asp-page="Index">Back to List</a></div> @section Scripts { @{await Html.RenderPartialAsync("_ValidationScriptsPartial");} }</body></html>
After code
AFTER @page @model pcore31.CreateModel @{ Layout = null; } <!DOCTYPE html><html><head><meta name="viewport" content="width=device-width" /><title>Create</title></head><body><h4>Pet</h4><hr /><div class="row"><div class="col-md-4"><form method="post"><div asp-validation-summary="ModelOnly" class="text-danger"></div><div class="form-group"><label asp-for="Pet.PetName" class="control-label"></label><input asp-for="Pet.PetName" class="form-control" /><span asp-validation-for="Pet.PetName" class="text-danger"></span></div><div class="form-group"><div class="form-group"><label asp-for="Pet.Dogpic" class="control-label"></label><div class="custom-file"><input asp-for="Pet.Dogpic" class="custom-file-input" id="customFile" type="file" width="100"><label class="custom-file-label" for="customFile">Choose file</label></div><span asp-validation-for="Pet.Dogpic" class="text-danger"></span></div></div>
<div class="form-group"><label asp-for="Pet.Odate" class="control-label"></label><input asp-for="Pet.Odate" class="form-control" /><span asp-validation-for="Pet.Odate" class="text-danger"></span></div><div class="form-group form-check"><label class="form-check-label"><input class="form-check-input" asp-for="Pet.Ocheck" /> @Html.DisplayNameFor(model => model.Pet.Ocheck)</label></div><div class="form-group"><input type="submit" value="Create" class="btn btn-primary" /></div></form></div></div><div><a asp-page="Index">Back to List</a></div> @section Scripts { @{await Html.RenderPartialAsync("_ValidationScriptsPartial");} }</body></html>
I only added in:
<div class="form-group"><div class="form-group"><label asp-for="Pet.Dogpic" class="control-label"></label><div class="custom-file"><input asp-for="Pet.Dogpic" class="custom-file-input" id="customFile" type="file" width="100"><label class="custom-file-label" for="customFile">Choose file</label></div><span asp-validation-for="Pet.Dogpic" class="text-danger"></span></div></div>
For the dialog to upload an image.
All works to that point:
But in the model behind:
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 pcore31.Models; namespace pcore31 { public class CreateModel : PageModel { private readonly pcore31.Models.DatabaseContext _context; public CreateModel(pcore31.Models.DatabaseContext context) { _context = context; } public IActionResult OnGet() { return Page(); } [BindProperty] public Pet Pet { get; set; } // To protect from overposting attacks, enable the specific properties you want to bind to, for // more details, see https://aka.ms/RazorPagesCRUD. public async Task<IActionResult> OnPostAsync() { if (!ModelState.IsValid) { return Page(); } HOW DO I CALL UploadedFile AND HOW TO INCLUDE PICNAME IN THE SAVE OPERATION, SINCE INDIVIDUAL FIELDS AREN'T USED IN THE GENERATED CODE ?????? _context.Pet.Add(Pet); await _context.SaveChangesAsync(); return RedirectToPage("./Index"); } public string UploadedFile(Pet model) { What code goes here??? } return picname; } }
Images are stored at wwwroot\images\upload.
Please, I just want to upload an image, and save the name to db. I already know how to display them.