asp.net core image file edit page

Anonymous
2023-07-14T07:24:05.7533333+00:00

I can create and delete images but cannot store images, please suggest.

Capturevisit.JPG

https://github.com/KalyanAllam/DoctorClinic

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,731 questions
{count} votes

Accepted answer
  1. JasonPan - MSFT 6,256 Reputation points Microsoft Vendor
    2023-07-14T08:39:13.77+00:00

    Hi @KALYANA ALLAM,

    If you want show the picture in your page, you can try the code below. If you want store it, you can check my first comment.

    Test Result
    User's image

    <div class="row">
    <div class="col-md-4">
    <form asp-action="Edit" enctype="multipart/form-data">
    <div asp-validation-summary="ModelOnly" class="text-danger"></div>
    <input type="hidden" asp-for="Visitid" />
    <div class="form-group">
    <label asp-for="Notes" class="control-label"></label>
    <input asp-for="Notes" class="form-control" />
    <span asp-validation-for="Notes" class="text-danger"></span>
    </div>
    <div class="form-group">
    <label asp-for="Visitdate" class="control-label"></label>
    <input asp-for="Visitdate" class="form-control" />
    <span asp-validation-for="Visitdate" class="text-danger"></span>
    </div>
    <div class="form-group">
    <label asp-for="Patientid" class="control-label"></label>
    <select asp-for="Patientid" class="form-control" asp-items="ViewBag.Patientid"></select>
    <span asp-validation-for="Patientid" class="text-danger"></span>
    </div>
    <div class="form-group">
    <label asp-for="Clinicianid" class="control-label"></label>
    <select asp-for="Clinicianid" class="form-control" asp-items="ViewBag.Clinicianid"></select>
    <span asp-validation-for="Clinicianid" class="text-danger"></span>
    </div>
    <div class="form-group">
    <label asp-for="ImageData" class="control-label"></label>
    <input name="image" type="file" class="form-control" />
    <span asp-validation-for="ImageData" class="text-danger"></span>
    </div>
    <div class="form-group">
    <input type="submit" value="Save" class="btn btn-primary" />
    </div>
    </form>
    </div>
    </div>
    
            [HttpPost]
            [ValidateAntiForgeryToken]
            public async Task<IActionResult> Edit(int id, [Bind("Visitid,Notes,Visitdate,Patientid,Clinicianid")] Visit visit, IFormFile image)
            {
                if (id != visit.Visitid)
                {
                    return NotFound();
                }
    
     
    
                if (image != null)
                {
                    if (image.Length > 0)
                    {
                        using (var target = new MemoryStream())
                        {
                            image.CopyTo(target);
                            visit.ImageData = target.ToArray();
                        }
                    }
                }
    
     
    
                if (ModelState.IsValid)
                {
                    try
                    {
                        _context.Update(visit);
                        await _context.SaveChangesAsync();
                    }
                    catch (DbUpdateConcurrencyException)
                    {
                        if (!VisitExists(visit.Visitid))
                        {
                            return NotFound();
                        }
                        else
                        {
                            throw;
                        }
                    }
                    return RedirectToAction(nameof(Index));
                }
                ViewData["Clinicianid"] = new SelectList(_context.Clinicians, "Clinicianid", "Clinicianid", visit.Clinicianid);
                ViewData["Patientid"] = new SelectList(_context.Patients, "Patientid", "Patientid", visit.Patientid);
                return View(visit);
            }
    

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,

    Jason Pan

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Anonymous
    2023-07-14T10:41:28.8166667+00:00

    JasonPan - MSFT

    Thanks for the code

    Case 1: When data and image are edited it works fine

    Case 2: When data is edited and image is not changed it is giving below error, I changed a bit to display the image while editing.

    Capturex.JPG

    Captureerror

    https://github.com/KalyanAllam/DoctorClinic


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.