0

I have a Edit form and a file upload inside in, if upload file has file, everything is normal, but when it's empty it passes null value to database. I use if to check file upload length but it doesn't work. I also remove LogoFile from Bind. here is my .cshtml code:

<form asp-action="Edit" enctype="multipart/form-data">
<div class="form-group">
                <label asp-for="FirstName" class="control-label"></label>
                <input asp-for="FirstName" class="form-control" />
                <span asp-validation-for="FirstName" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="LastName" class="control-label"></label>
                <input asp-for="LastName" class="form-control" />
                <span asp-validation-for="LastName" class="text-danger"></span>
            </div>
<div class="form-group">
                <label asp-for="LogoFile" class="control-label"></label><br />
                <input asp-for="LogoFile" type="file" class="form-control" />
                <img src="/@Html.DisplayFor(model => model.LogoFile)"/>
                <span asp-validation-for="LogoFile" class="text-danger"></span>
            </div>
 <div class="form-group">
                <input type="submit" value="save" class="btn btn-green" />
            </div>
        </form>

and here is my controller code:

 [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Edit(int id, [Bind("Id,FirstName,LastName")] Profile profile,IFormFile LogoFile)
    {

        if (id != profile.Id)
        {
            return NotFound();
        }

        if (ModelState.IsValid)
        {
            try
            {
                if (LogoFile != null || LogoFile.Length != 0)

                {
                    var path = "images/" + LogoFile.FileName;

                    using (var stream = new FileStream("wwwroot/" + path, FileMode.Create))
                    {
                        await LogoFile.CopyToAsync(stream);
                        profile.LogoFile = path.ToString();
                    }
                }
                _context.Update(profile);
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ProfileExists(profile.Id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }
            return RedirectToAction(nameof(Index));
        }
        return View(profile);
    }
Nashmár
  • 392
  • 1
  • 7
  • 22
MohammadReza
  • 33
  • 2
  • 9
  • 2
    You do not use data models in your view when editing data - you use a view model (and that model will contain a `IFormFile` property to bind to). When you post the view model, you get the data model from the data base and update its properties. In the case of the file being `null`, you do not update the property so the original value is retained. Refer [What is ViewModel in MVC?](https://stackoverflow.com/questions/11064316/what-is-viewmodel-in-mvc) –  Jun 08 '18 at 06:57
  • Thank you for your response. in my form user may upload file and my not. it's optional. I don't want to remove upload file at all in my edit mode. – MohammadReza Jun 08 '18 at 07:04
  • 1
    Do you even read my comment? I know its optional! I never said to remove the file input. –  Jun 08 '18 at 07:06
  • Sorry I made a mistake and I'm newbie in asp.core! you mean I used a viewmodel? I used a data model for this edit form. when I use viewmodel I get this error : `The model item passed into the ViewDataDictionary is of type 'MyWebApp.Models.Profile', but this ViewDataDictionary instance requires a model item of type 'MyWebApp.Models.AdminPanelViewModel.ProfileViewModel'.` – MohammadReza Jun 08 '18 at 07:15
  • That is because you are passing a `Profile` model to a view that expects a `ProfileViewModel` modl (read the link I gave you) –  Jun 08 '18 at 07:18

0 Answers0