Write a code to Develop a feature for an ASP.NET MVC 6 web application. Create an upload image where users can upload images. 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.
It is showing no error and running fine, but it is not saving in Images folder, no path is saved in ImagePath in SolutionImages and also no content is saved in Solutions.
SolutionsController.cs:
public async Task<IActionResult> Add(AddSolutionViewModel addSolutionRequest)
{
if (addSolutionRequest.Image != null && addSolutionRequest.Image.Length > 0)
{
var fileName = Guid.NewGuid().ToString() + Path.GetExtension(addSolutionRequest.Image.FileName);
var filePath = Path.Combine("Images", fileName);
using (var fileStream = new FileStream(filePath, FileMode.Create))
{
await addSolutionRequest.Image.CopyToAsync(fileStream);
}
var solutionImage = new SolutionImage()
{
SolutionId = addSolutionRequest.Id,
ImagePath = "/Images/" + fileName
};
mvcDemoDbContext.SolutionImages.Add(solutionImage);
await mvcDemoDbContext.SaveChangesAsync();
var solution = new Solutions()
{
ProblemStatement = addSolutionRequest.ProblemStatement,
Module = addSolutionRequest.Module,
RootCause = addSolutionRequest.RootCause,
Proposedsolutions = addSolutionRequest.Proposedsolutions,
SolutionImages = new List<SolutionImage> { solutionImage }
};
await mvcDemoDbContext.Solutions.AddAsync(solution);
await mvcDemoDbContext.SaveChangesAsync();
return RedirectToAction("Index");
}
return View(addSolutionRequest);
}
AddSolutionViewModel.cs:
using demo.Models.Domain;
using System.ComponentModel.DataAnnotations.Schema;
namespace demo.Models
{
public class AddSolutionViewModel
{
public int Id { get; set; }
[Column("Problem Statement")]
public string? ProblemStatement { get; set; }
[Column("Problem Description")]
public string? ProblemDescription { 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<SolutionImage>? SolutionImages { get; set; }
[NotMapped]
public IFormFile Image { get; set; }
}
}
Solutions.cs:
using System.ComponentModel.DataAnnotations.Schema;
namespace demo.Models.Domain
{
public class Solutions
{
public int Id { get; set; }
[Column("Problem Statement")]
public string? ProblemStatement { get; set; }
[Column("Problem Description")]
public string? ProblemDescription { 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<SolutionImage> SolutionImages { get; set; }
}
}
SolutionImages.cs:
using System.ComponentModel.DataAnnotations.Schema;
namespace demo.Models.Domain
{
public class SolutionImage
{
[Column("ImageID")]
public int Id { get; set; }
public int SolutionId { get; set; }
[ForeignKey("SolutionId")]
public Solutions Solution { get; set; }
[Column("ImagePath")]
public string ImagePath { get; set; }
}
}
Add.cshtml:
@model demo.Models.AddSolutionViewModel
<!DOCTYPE html>
<html>
<head>
<title>My Form</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<form method="post" class="form">
<div class="form-group">
<label asp-for="ProblemStatement" class="control-label"></label>
<input asp-for="ProblemStatement" class="form-control" />
<span asp-validation-for="ProblemStatement" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="ProblemDescription" class="control-label"></label>
<input asp-for="ProblemDescription" class="form-control" />
<span asp-validation-for="ProblemDescription" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Module" class="control-label"></label>
<input asp-for="Module" class="form-control" />
<span asp-validation-for="Module" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="RootCause" class="control-label"></label>
<input asp-for="RootCause" class="form-control" />
<span asp-validation-for="RootCause" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Proposedsolutions" class="control-label"></label>
<input asp-for="Proposedsolutions" class="form-control" />
<span asp-validation-for="Proposedsolutions" class="text-danger"></span>
</div>
<div class="mb-3">
<label for="image" class="form-label" style="font-family: Alstom; color: #1d3245; font-size: 20px;">Image</label>
<input type="file" class="form-control" name="image" id="image" accept="image/*">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
<script src="https://ajax.aspnetcdn.com/ajax/jquery/jquery-3.5.1.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
</body>
</html>