Upload an image in a Folder through ASP NET Core Razor Pages using Visual studio 2022 | Part 11

Опубликовано: 17 Март 2026
на канале: Syed Ali
142
1

Upload an image in a Folder through ASP.NET Core Razor Pages using Visual studio 2022:
Requirement: SQL server 2012, Visual studio 2022
Objective: Images will be upload in the folder and path will be save in the database.
Input Form Design:

Out Put: get the data from page and value will be saved in database.
SQL table:
CREATE TABLE [emp_detail](
[emp_id] [int] IDENTITY(100,1) NOT NULL,
[emp_name] [nvarchar](100) NULL,
[address] [nvarchar](200) NULL,
[photoFileName] [nvarchar](200) NULL,
CONSTRAINT [PK_emp_detail] PRIMARY KEY CLUSTERED
(
[emp_id] ASC
)
)

Step:1
Installation Required through NuGet packages
1) Microsoft.EntityFrameworkCore.SqlServer
2) Microsoft.entityframeworkcore.tools
3) Microsoft.Extensions.Configuration

Step:2
Create a folder Models then Create a Class of DB context through the command line of Nuget Package:
Scaffold-DbContext "Server=DESKTOP-HSSNFCU;Initial Catalog=Sample_db;User ID=sa;Password=1234; TrustServerCertificate=true;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models
-force (it will use when any changes occurs in table/stored procedures.)
Step:3
// add db context to the container
builder.Services.AddDbContext SampleDbContext ();

Step:4
Create a view model “EmpDetailViewModel” to capture the data from form to properties, so that we can get the data and save the data into database
public class EmpDetailViewModel
{
public string? EmpName { get; set; }
public IFormFile? FormFile { get; set; }
}

Step:5
Need to added the Razor pages: