help me to write a code to Develop a feature for an ASP.NET MVC 6 web application.
Create an image upload option in Edit where users can also new upload images with already present images without deleting it and should change the text also if there is any change.
Once an image is uploaded, it should be saved in a folder named 'Images.' Additionally, the path to the uploaded image should be stored in the 'ImagePath' column in the 'dbo.SolutionImage' table, associating it with a specific 'SolutionId.'
Please provide the necessary code and instructions to implement this feature, ensuring that the uploaded images are stored and associated correctly.
solutioncontroller.cs:
using one.Data;
using one.Models;
using one.Models.Domain;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.CodeAnalysis;
using Microsoft.EntityFrameworkCore;
using static System.Net.Mime.MediaTypeNames;
using System.Reflection.Metadata.Ecma335;
namespace one.Controllers
{
public class SolutionsController : Controller
{
private readonly MVCDemoDbContext mvcDemoDbContext;
public SolutionsController(MVCDemoDbContext mvcDemoDbContext)
{
this.mvcDemoDbContext = mvcDemoDbContext;
}
[HttpGet]
public async Task<IActionResult> Index()
{
var solutions = await mvcDemoDbContext.Solutions.ToListAsync();
return View(solutions);
}
[HttpGet]
public async Task<IActionResult> View(int id)
{
var solution = await mvcDemoDbContext.Solutions.FirstOrDefaultAsync(x => x.Id == id);
if (solution != null)
{
var viewModel = new UpdateSolution()
{
Id = solution.Id,
ProblemStatement = solution.ProblemStatement,
Module = solution.Module,
RootCause = solution.RootCause,
Proposedsolutions = solution.Proposedsolutions,
};
return await Task.Run(() => View("View", viewModel));
}
return RedirectToAction("Index");
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> View(int id, UpdateSolution model, IFormFile[] Images)
{
if (ModelState.IsValid)
{
var existingSolution = await mvcDemoDbContext.Solutions
.Include(s => s.SolutionImages)
.FirstOrDefaultAsync(s => s.Id == id);
if (existingSolution != null)
{
existingSolution.ProblemStatement = model.ProblemStatement;
existingSolution.Module = model.Module;
existingSolution.RootCause = model.RootCause;
existingSolution.Proposedsolutions = model.Proposedsolutions;
if (Images != null && Images.Length > 0)
{
foreach (var image in Images)
{
var fileName = Path.GetFileName(image.FileName);
var filePath = Path.Combine(Directory.GetCurrentDirectory(), "Images", fileName);
using (var fileStream = new FileStream(filePath, FileMode.Create))
{
await image.CopyToAsync(fileStream);
}
var solutionImage = new SolutionImages
{
SolutionId = id,
ImagePath = $"/Images/{fileName}"
};
existingSolution.SolutionImages.Add(solutionImage);
}
}
await mvcDemoDbContext.SaveChangesAsync();
}
return RedirectToAction(nameof(Index));
}
return RedirectToAction(nameof(Index));
}
}
}
View.cshtml:
@model one.Models.UpdateSolution
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<div style="margin-top:110px;">
<h1 style="font-family:Alstom;color:#1d3245">Add new data</h1>
<form method="post" action="View" class="mt-5" enctype="multipart/form-data">
@Html.AntiForgeryToken()
<div class="mb-3">
<label for="" class="form-label" style="font-family:Alstom;color:#1d3245;font-size:20px;">Id</label>
<input type="text" class="form-control" asp-for="Id" readonly />
</div>
<div class="mb-3">
<label for="" class="form-label" style="font-family:Alstom;color:#1d3245;font-size:20px;">Problem Statement</label>
<input type="text" class="form-control" asp-for="ProblemStatement" />
</div>
<div class="mb-3">
<label for="" class="form-label" style="font-family:Alstom;color:#1d3245;font-size:20px;">Module</label>
<input type="text" class="form-control" asp-for="Module" />
</div>
<div class="mb-3">
<label for="" class="form-label" style="font-family:Alstom;color:#1d3245;font-size:20px;">Root Cause</label>
<input type="text" class="form-control" asp-for="RootCause" />
</div>
<div class="mb-3">
<label for="" class="form-label" style="font-family:Alstom;color:#1d3245;font-size:20px;">Proposed solution</label>
<input type="text" class="form-control" asp-for="Proposedsolutions" />
</div>
<div class="form-group">
<label asp-for="Images" class="control-label"></label>
<input asp-for="Images" type="file" multiple class="form-control" />
<span asp-validation-for="Images" class="text-danger"></span>
</div>
<button type="submit" class="btn btn-primary" style="margin-top:15px;background-color:#1d3245;">Submit</button>
</form>
</div>
</body>
</html>
UpdateSolution.cs:
using one.Models.Domain;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace one.Models
{
public class UpdateSolution
{
public int Id { get; set; }
[Column("Problem Statement")]
public string ProblemStatement { get; set; }
[Column("Module")]
public string Module { get; set; }
[Column("Root Cause")]
public string RootCause { get; set; }
[Column("Proposed solutions")]
public string Proposedsolutions { get; set; }
[NotMapped]
public List<SolutionImages> SolutionImages { get; set; }
[Required(AllowEmptyStrings = true)]
public IFormFile[]? Images { get; set; }
}
}
above code snippet is not saving edited data and reflecting in index.
help me