adding photo and model

2019-08-18 06:51发布

问题:

I am new to asp.net core. I am facing a problem when uploading a file(image) with my model.

the viewmodel :

namespace WebApplication1.ViewModels
{
    public class HotelViewModel
    {
        public String NomHotel { get; set; }
        public String NomAgence { get; set; }
        public String Filepath{ get; set; }
    }
}

this is the cshtml :

@model WebApplication1.ViewModels.HotelViewModel
<form novalidate method="post" enctype="multipart/form-data" asp-
controller="Hotel" asp-action="Hotel">
<div id="parent">
    <div id="wide" class="col-lg-4">
        <div asp-validation-summary="ModelOnly"></div>
        <div class="form-group">
            <label asp-for="NomHotel"></label>
            <input asp-for="NomHotel" class="form-control" />
            <span asp-validation-for="NomHotel"></span>
        </div>
        <div class="form-group">
            <label asp-for="NomAgence"></label>
            <input asp-for="NomAgence" class="form-control" />
            <span asp-validation-for="NomAgence"></span>
        </div>
        <div class="col-md-10">
            <p>Upload one or more files using this form:</p>
            <input type="file" name="files" multiple class="btn" />
        </div>
        <div id="#rest">
            <div class="form-group">
                <input type="submit" value="Ajouter" class="btn btn-lg btn-
 success ahbat" />
            </div>
        </div>
    </div>
</div>

and this is the controller :

[HttpPost]
public IActionResult Hotel(HotelViewModel mod)
{
    Hotel hotel = new Hotel
    {
        NomAgence = mod.NomAgence,
        NomHotel = mod.NomHotel
    };

    var newFileName = string.Empty;
    if (HttpContext.Request.Form.Files != null)
    {
        var fileName = string.Empty;
        string PathDB = string.Empty;

        var files = HttpContext.Request.Form.Files;

        foreach (var file in files)
        {
            if (file.Length > 0)
            {
                //Getting FileName
                fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');

                //Assigning Unique Filename (Guid)
                var myUniqueFileName = Convert.ToString(Guid.NewGuid());

                //Getting file Extension
                var FileExtension = Path.GetExtension(fileName);

                // concating  FileName + FileExtension
                newFileName = myUniqueFileName + FileExtension;

                // Combines two strings into a path.
                fileName = Path.Combine(_environment.WebRootPath, "HotelImg") + $@"\{newFileName}";

                // if you want to store path of folder in database
                PathDB = "demoImages/" + newFileName;

                hotel.Piscine = newFileName;
                using (FileStream fs = System.IO.File.Create(fileName))
                {
                    file.CopyTo(fs);
                    fs.Flush();
                }
            }
        }
    }

    IH.Create(hotel);
    return View();
}

as said I have tried different combinations of code but I always get the file or the model never both and I need both of them. This is the most correct code I can show you sorry if it is not too clear.