Subscribe Us

LightBlog

Thursday, 23 December 2021

December 23, 2021

BATCH SCRIPT TO DELETE TEMP FILES AND CLEAR CACHE

@echo off

@echo clear ram cache , temp files boost computer speed

title This file is cleaning and Removing the temp files

color 0a

tree c:\windows\temp

del /s /f /q c:\windows\temp\*.*

tree c:\windows\prefetch

del /s /f /q c:\windows\prefetch\*.*

tree %userprofile%\appdata\local\temp

del /s /f /q %userprofile%\appdata\local\temp\*.*

%windir%\system32\rundll32.exe advapi32.dll,ProcessIdleTasks

@echo temp and windows memory cache file deleted successfully , press any key to continue....

pause > nul

Tuesday, 14 December 2021

December 14, 2021

Hashing with SHA1 Algorithm in C#


For those who want a "standard" text formatting of the hash, you can use something like the following:
static string Hash(string input)
{
    using (SHA1Managed sha1 = new SHA1Managed())
    {
        var hash = sha1.ComputeHash(Encoding.UTF8.GetBytes(input));
        var sb = new StringBuilder(hash.Length * 2);

        foreach (byte b in hash)
        {
            // can be "x2" if you want lowercase
            sb.Append(b.ToString("X2"));
        }

        return sb.ToString();
    }
}

Saturday, 1 May 2021

May 01, 2021

CRUD Operation in Asp.Net Core Web API with Swagger

Introduction

In this article, I explain how to integrate our API with the swagger and execute our web API using swagger, In this article, I explain CRUD operation with a Repository pattern in .net Core Web API.

Step 1: Create new Web API with using .Net cores

Step 2: Now install the package of swagger

Open your NuGet package manager console and write below command into it.

Install-Package Swashbuckle

if you need more details about this package please click here (https://www.nuget.org/packages/Swashbuckle/)

Step 3: Register the swagger generator

now open the startup.cs file and go to ConfigureServices, add below code into it.

services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo
                {
                    Version = "v1",
                    Title = "Employee API",
                    Description = "A simple example ASP.NET Core Web API",
                    TermsOfService = new Uri("https://example.com/terms"),
                    Contact = new OpenApiContact
                    {
                        Name = "abc",
                        Email = "abc@gmail.com",
                    }                    
                });
            });

now go to the Configure in the same file and Enable middleware to serve generated Swagger as a JSON endpoint, paste the below code into it.

// Enable middleware to serve generated Swagger as a JSON endpoint.
           app.UseSwagger();
           // Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.),
           // specifying the Swagger JSON endpoint.
           app.UseSwaggerUI(c =>
           {
               c.SwaggerEndpoint("/swagger/v1/swagger.json", "Employee.API V1");
               c.RoutePrefix = string.Empty;
           });
now build your web API and run it.

Step 4: Create a folder name is “Models” in your web API, right-click on the models and create a new class for the model.
make a model like given below code.

using System.ComponentModel.DataAnnotations;
namespace SwaggerDemoWebAPI.Models
{
    public class MEmployee
    {
        [Key]
        public int EmpID { get; set; }
        public string Name { get; set; }
        public string Email { get; set; }
        public string Phone { get; set; }
        public string Gender { get; set; }
        public string Password { get; set; }
    }
}
Step 5: Create a folder name is “Data” in your web API, right-click on the data and create a new class for the DB context.

now add the DB set in the DB context like give below code.

using Microsoft.EntityFrameworkCore;
using SwaggerDemoWebAPI.Models;
namespace Employee.API
{
    public class DBContext : DbContext
    {
        public DBContext(DbContextOptions<DBContext> options) : base(options) { }
        public DbSet<MEmployee> Emp { get; set; }
    }
}
Step 6: Make the connection
now open the appsettings.json file and add the connection string like given below code.

{
  "ConnectionStrings": {
    "IdentityConnection": "Data Source=abc\\SQLEXPRESS01;Initial Catalog=Employee;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"
  },
  "CatalogBaseUrl": "",
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Warning",
      "Microsoft": "Warning",
      "System": "Warning"
    }
  }
}
Now we need to configure the connection string so please open the startups.cs file and go to the configure services, paste below line at the first

// Add Identity DbContext
 services.AddDbContext<DBContext>(options =>options.UseSqlServer(Configuration.GetConnectionString("IdentityConnection")));
make sure your connection string name same in both file (appsettings.json & startup.cs)

Step 7: Add repository in our web API project, so you need to create a new folder Repository in your Web API project

In the repository create one interface like “IEmployeeRepository” please write the name of your register starting with I if that is interface then, copy below code and paste into your interface.

using SwaggerDemoWebAPI.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace SwaggerDemoWebAPI.Repository
{
   public interface IEmployeeRepository
    {
        void Create(MEmployee item);
        MEmployee GetById(int Id);
        List<MEmployee> GetAll();
        void Update(int id,MEmployee item);
        void Delete(int Id);
    }
}
now add the one class for the employee repository, copy below code into your employee repository

using System.Collections.Generic;
using System.Linq;
using Employee.API;
using SwaggerDemoWebAPI.Models;
namespace SwaggerDemoWebAPI.Repository
{
    public class EmployeeRepository : IEmployeeRepository
    {
        private readonly DBContext _context;
        public EmployeeRepository(DBContext context)
        {
            _context = context;
        }
        public void Create(MEmployee item)
        {
            _context.Emp.Add(item);
            _context.SaveChanges();
        }
        public MEmployee GetById(int Id)
        {
            return _context.Emp.FirstOrDefault(t => t.EmpID == Id);
        }
        public List<MEmployee> GetAll()
        {
            return _context.Emp.ToList();
        }
        public void Update(int id,MEmployee item)
        {
            var entity = _context.Emp.Find(id);
            if (entity == null)
            {
                return;
            }
            _context.Entry(entity).CurrentValues.SetValues(item);
            _context.SaveChanges();
        }
        public void Delete(int id)
        {
            var entity = _context.Emp.Find(id);
            if (entity == null)
            {
                return;
            }
            _context.Emp.Remove(entity);
            _context.SaveChanges();
        }
    }
}
Step 8: Create Employee API, now you need to create a controller in your web API controller folder, copy below code and paste into your controller

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using SwaggerDemoWebAPI.Models;
using SwaggerDemoWebAPI.Repository;
namespace SwaggerDemoWebAPI.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class EmployeeController : ControllerBase
    {
        public EmployeeController(IEmployeeRepository emp)
        {
            _Emp = emp;
        }
        public IEmployeeRepository _Emp { get; set; }
        [HttpPost]
        public void Post([FromBody] MEmployee modal)
        {
            _Emp.Create(modal);
        }
        [HttpGet("{id}")]
        public IActionResult Get(int id)
        {
            var item = _Emp.GetById(id);
            if (item == null)
            {
                return NotFound();
            }
            return new ObjectResult(item);
        }
        [HttpGet]
        public List<MEmployee> Get()
        {   
            return _Emp.GetAll();
        }
        [HttpPut("{id}")]
        public void Put(int id, [FromBody] MEmployee modal)
        {
            _Emp.Update(id, modal);
        }
        [HttpDelete("{id}")]
        public void Delete(int id)
        {
            _Emp.Delete(id);
        }
    }
}
make sure don’t ambiguous the method, after all code complete please rebuild your solution explorer.

now Run your web API and test your web API with the swagger.

Tuesday, 13 April 2021

Sunday, 21 February 2021

February 21, 2021

Convert Any String Date Format To Common Date Format In C#

In this post, we will see how we can accept any date format and return with a common date format with the TryParseExact() method. Converting date to the wanted date format is a time spending and slow process in several languages including C#.

TryParseExact includes the following overload methods

DateTime.TryParseExact(string value, string format, IFormatProvider provider, DateTimeStyles style, out DateTime result)

Value: It is a string representation of a date.

Format: It is a format specifier that specifies a date look like after conversion.

Provider: It is an object which specifies culture info.

style: It is an enum DateTimeStyles.

result: return formatted date.

In the below code, I created one method namely ChangeDateFormat which accepts string date in any format and will return with fix format i.e. “MM/dd/yyyy” format.

public string ChangeDateFormat(string inputDate)

{

    string[] formats = { "dd/MM/yyyy", "dd/M/yyyy", "d/M/yyyy",  "d/MM/yyyy",

                     "dd/MM/yy", "dd/M/yy", "d/M/yy", "d/MM/yy", "MM/dd/yyyy",

                         "yyyy/MM/dd","dd/MM/yyyy","MM-dd-yyyy","yyyyMMdd"

                         };

    DateTime.TryParseExact(inputDate, formats,

    System.Globalization.CultureInfo.InvariantCulture,

    System.Globalization.DateTimeStyles.None, out DateTime outputDate);

    return outputDate.ToString("MM/dd/yyyy");

}

In the above method need to pass all possible format in string[] which we wanna accept. we have done now you can use this method anywhere in your project.

February 21, 2021

CRUD Operations In .NET Core 3.0 With Visual Studio 2019

    In this article, we’ll learn how to perform CRUD operations with .NET Core 3.0 and Visual Studio 2019. We will use a dapper to perform CRUD operations.

Recommended Prerequisites

  • .NET Core 3.0 (Download from here)
  •  Visual Studio 2019. (Download from here)

    Create a Database, Tables, and Stored Procedures.

    First, we need to create a SQL Server database, tables, and stored procedure which we need to use in the application.

     Here, I’m creating a database, “CoreMaster” and a table “Jobs”.

     CREATE TABLE [dbo].[Job](

    [JobID] [int] IDENTITY(1,1) NOT NULL,

    [JobTitle] [nchar](250) NULL,

    [JobImage] [nvarchar](max) NULL,

    [CityId] [int] NULL,

    [IsActive] [bit] NULL,

    [CreatedBY] [nvarchar](50) NULL,

    [CreatedDateTime] [datetime] NULL,

    [UpdatedBY] [nvarchar](50) NULL,

    [UpdatedDateTime] [datetime] NULL,

    CONSTRAINT [PK_Job] PRIMARY KEY CLUSTERED

    (

    [JobID] ASC

    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

    ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

    Now, I’m going to create stored procedures for adding jobs, fetching job lists, and updating jobs.

    The “Add Job” stored procedure is named “[SP_Add_Job]” which returns the inserted record’s job Id.

    CREATE PROCEDURE [dbo].[SP_Add_Job]

    @JobTitle NVARCHAR(250) ,

    @JobImage NVARCHAR(Max) ,

    @CityId int ,

    @IsActive BIT ,

    @CreatedBY NVARCHAR(50) ,

    @CreatedDateTime DATETIME ,

    @UpdatedBY NVARCHAR(50),

    @UpdatedDateTime DATETIME

    AS

    BEGIN

    DECLARE @JobId as BIGINT

    INSERT INTO [Job]

    (JobTitle ,

    JobImage ,

    CityId ,

    IsActive ,

    CreatedBY ,

    CreatedDateTime ,

    UpdatedBY ,

    UpdatedDateTime

    )

    VALUES ( @JobTitle ,

    @JobImage ,

    @CityId ,

    @IsActive ,

    @CreatedBY ,

    @CreatedDateTime ,

    @UpdatedBY ,

    @UpdatedDateTime

    );

    SET @JobId = SCOPE_IDENTITY();

    SELECT @JobId AS JobId;

    END;

    Top of Form

    The “Update job” stored procedure is “[SP_Update_Job]” 

    CREATE PROCEDURE [dbo].[SP_Update_Job]

    @JobId INT,

    @JobTitle NVARCHAR(250) ,

    @JobImage NVARCHAR(Max) ,

    @CityId INT ,

    @IsActive BIT ,

    @UpdatedBY NVARCHAR(50),

    @UpdatedDateTime DATETIME

    AS

    BEGIN

    UPDATE job

    SET

    job.JobTitle = @JobTitle,

    job.JobImage = @JobImage ,

    job.CityId = @CityId ,

    job.IsActive = @IsActive ,

    job.UpdatedBY = @UpdatedBY ,

    job.UpdatedDateTime = @UpdatedDateTime

    FROM [Job] job

    WHERE JobId = @JobId

    END;

    The “Fetch job list” store procedure is [SP_Job_List].

     

    CREATE PROCEDURE [dbo].[SP_Job_List]

    AS

    BEGIN

    SET NOCOUNT ON;

    select * from [Job]

    END

    Open Visual Studio 2019

    Go to the Start menu on your Windows desktop and type Visual studio 2019; open it.

    Create a new project

Click on the “ASP.NET Core Web Application” and press “Next”.

 

From the wizard, select “Web Application (Model-View-Controller)”. The framework must be selected as .NET Core 3.0. Then, click on the “OK” button.

Put an appropriate project name and select the location where you want to create this project. Again, click the “Create” button.

Now “Build” the project. It will install .NET Core 3.0 runtime (if it not installed on the machine).

 Install NuGet packages.

We need to install the below packages.

1.    Microsoft.EntityFrameworkCore.SqlServer

2. Microsoft.EntityFrameworkCore.SqlServer.Design

3. Microsoft.EntityFrameworkCore.Tools

4. Dapper

Create Dapper Class and Interface

Now, create two folders –

1.    Helper

2.    Interface

In the Interface folder, add a new interface namely “IDapperHelper” and copy the below code and paste in that class.

using Dapper;

using System;

using System.Collections.Generic;

using System.Data;

using System.Data.Common;

using System.Linq;

using System.Linq.Expressions;

using System.Threading.Tasks;

namespace CoreDemo_3_0.Interfaces

{

public interface IDapperHelper : IDisposable

{

DbConnection GetConnection();

T Get<T>(string sp, DynamicParameters parms, CommandType commandType = CommandType.StoredProcedure);

List<T> GetAll<T>(string sp, DynamicParameters parms, CommandType commandType = CommandType.StoredProcedure);

int Execute(string sp, DynamicParameters parms, CommandType commandType = CommandType.StoredProcedure);

T Insert<T>(string sp, DynamicParameters parms, CommandType commandType = CommandType.StoredProcedure);

T Update<T>(string sp, DynamicParameters parms, CommandType commandType = CommandType.StoredProcedure);

}

}

Top of Form

In the Helper folder, add a new class namely “DapperHelper” and copy the below code and paste in that class.

using System;

using System.Collections.Generic;

using System.Linq;

using Dapper;

using Microsoft.Extensions.Configuration;

using System.Data;

using System.Data.Common;

using System.Data.SqlClient;

using System.Linq.Expressions;

using static Dapper.SqlMapper;

using CoreDemo_3_0.Interfaces;

namespace CoreDemo_3_0.Helper

{

public class DapperHelper : IDapperHelper

{

private readonly IConfiguration _config;

public DapperHelper(IConfiguration config)

{

_config = config;

}

public DbConnection GetConnection()

{

return new SqlConnection(_config.GetConnectionString("DefaultConnection"));

}

public T Get<T>(string sp, DynamicParameters parms, CommandType commandType = CommandType.StoredProcedure)

{

using (IDbConnection db = new SqlConnection(_config.GetConnectionString("DefaultConnection")))

{

return db.Query<T>(sp, parms, commandType: commandType).FirstOrDefault();

}

}

public List<T> GetAll<T>(string sp, DynamicParameters parms, CommandType commandType = CommandType.StoredProcedure)

{

using (IDbConnection db = new SqlConnection(_config.GetConnectionString("DefaultConnection")))

{

return db.Query<T>(sp, parms, commandType: commandType).ToList();

}

}

public int Execute(string sp, DynamicParameters parms, CommandType commandType = CommandType.StoredProcedure)

{

using (IDbConnection db = new SqlConnection(_config.GetConnectionString("DefaultConnection")))

{

return db.Execute(sp, parms, commandType: commandType);

}

}

public T Insert<T>(string sp, DynamicParameters parms, CommandType commandType = CommandType.StoredProcedure)

{

T result;

using (IDbConnection db = new SqlConnection(_config.GetConnectionString("DefaultConnection")))

{

try

{

if (db.State == ConnectionState.Closed)

db.Open();

using (var tran = db.BeginTransaction())

{

try

{

result = db.Query<T>(sp, parms, commandType: commandType, transaction: tran).FirstOrDefault();

tran.Commit();

}

catch (Exception ex)

{

tran.Rollback();

throw ex;

}

}

}

catch (Exception ex)

{

throw ex;

}

finally

{

if (db.State == ConnectionState.Open)

db.Close();

}

return result;

}

}

public T Update<T>(string sp, DynamicParameters parms, CommandType commandType = CommandType.StoredProcedure)

{

T result;

using (IDbConnection db = new SqlConnection(_config.GetConnectionString("DefaultConnection")))

{

try

{

if (db.State == ConnectionState.Closed)

db.Open();

using (var tran = db.BeginTransaction())

{

try

{

result = db.Query<T>(sp, parms, commandType: commandType, transaction: tran).FirstOrDefault();

tran.Commit();

}

catch (Exception ex)

{

tran.Rollback();

throw ex;

}

}

}

catch (Exception ex)

{

throw ex;

}

finally

{

if (db.State == ConnectionState.Open)

db.Close();

}

return result;

}

}

public void Dispose()

{

throw new NotImplementedException();

}

}

}

Here, we created a Dapper helper which we will use to communicate with the database.

Create a Context class

Create a new folder named “Context” and add one class “DataContext” which extends from the DbContext class.

 

Copy the below code and paste inside the “DataContext” class.

using CoreDemo_3_0.Entities;

using Microsoft.EntityFrameworkCore;

using System;

using System.Collections.Generic;

using System.Linq;

using System.Threading.Tasks;

namespace CoreDemo_3_0.Context

{

public class DataContext : DbContext

{

public DataContext() { }

public DataContext(DbContextOptions<DataContext> options) : base(options) { }

}

}

 

Create one folder named “Entities” and add a class, namely “Job”.

Copy the below code and paste into the “Job” class.

using System;

using System.Collections.Generic;

using System.ComponentModel.DataAnnotations;

using System.Linq;

using System.Threading.Tasks;

namespace CoreDemo_3_0.Entities

{

public class Job

{

[Key]

public int JobID { get; set; }

public string JobTitle { get; set; }

public int CityId { get; set; }

public string JobImage { get; set; }

public bool IsActive { get; set; }

public string CreatedBY { get; set; }

public DateTime? CreatedDateTime { get; set; }

public string UpdatedBY { get; set; }

public DateTime? UpdatedDateTime { get; set; }

}

}

 

Create one folder named “Models” and add a class “JobModel”.

Copy the below code and paste into the “JobModel” class.

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Threading.Tasks;

namespace CoreDemo_3_0.Models

{

public class JobModel

{

public int JobID { get; set; }

public string JobTitle { get; set; }

public int CityId { get; set; }

public string JobImage { get; set; }

public bool IsActive { get; set; }

public string CreatedBY { get; set; }

public DateTime? CreatedDateTime { get; set; }

public string UpdatedBY { get; set; }

public DateTime? UpdatedDateTime { get; set; }

public int JobState { get; set; }

}

}

Add IJob interface

Add IJob interface, “IJobService” inside the interface folder which we created before. Below is the code of the interface.

using CoreDemo_3_0.Entities;

using System;

using System.Collections.Generic;

using System.Linq;

using System.Threading.Tasks;

namespace CoreDemo_3_0.Interfaces

{

public interface IJobService

{

int Delete(int JobId);

Job GetByJobId(int JobId);

string Update(Job job);

int Create(Job JobDetails);

List<Job> ListAll();

}

}

Add Job service

Add a new folder named “Services” and one class inside that folder. The class name will be “JobService”. Below is the JobService class code.

using CoreDemo_3_0.Entities;

using CoreDemo_3_0.Interfaces;

using Dapper;

using System;

using System.Collections.Generic;

using System.Data;

using System.Linq;

using System.Threading.Tasks;

using System.Transactions;

namespace CoreDemo_3_0.Services

{

public class JobService : IJobService

{

private readonly IDapperHelper _dapperHelper;

public JobService(IDapperHelper dapperHelper)

{

this._dapperHelper = dapperHelper;

}

public int Create(Job job)

{

var dbPara = new DynamicParameters();

dbPara.Add("JobTitle", job.JobTitle, DbType.String);

dbPara.Add("JobImage", job.JobImage, DbType.String);

dbPara.Add("CityId", job.CityId, DbType.Int32);

dbPara.Add("IsActive", job.IsActive, DbType.String);

dbPara.Add("CreatedBY", "1", DbType.String);

dbPara.Add("CreatedDateTime", DateTime.Now, DbType.DateTime);

dbPara.Add("UpdatedBY", "1", DbType.String);

dbPara.Add("UpdatedDateTime", DateTime.Now, DbType.DateTime);

#region using dapper

var data = _dapperHelper.Insert<int>("[dbo].[SP_Add_Job]",

dbPara,

commandType: CommandType.StoredProcedure);

return data;

#endregion

}

public Job GetByJobId(int JobId)

{

#region using dapper

var data = _dapperHelper.Get<Job>($"select * from job where JobId={JobId}", null,

commandType: CommandType.Text);

return data;

#endregion

}

public int Delete(int JobId)

{

var data = _dapperHelper.Execute($"Delete [Job] where JObId={JobId}", null,

commandType: CommandType.Text);

return data;

}

public List<Job> ListAll()

{

var data = _dapperHelper.GetAll<Job>("[dbo].[SP_Job_List]", null, commandType: CommandType.StoredProcedure);

return data.ToList();

}

public string Update(Job job)

{

var dbPara = new DynamicParameters();

dbPara.Add("JobTitle", job.JobTitle, DbType.String);

dbPara.Add("JobId", job.JobID);

dbPara.Add("JobImage", job.JobImage, DbType.String);

dbPara.Add("CityId", job.CityId, DbType.Int32);

dbPara.Add("IsActive", job.IsActive, DbType.String);

dbPara.Add("UpdatedBY", "1", DbType.String);

dbPara.Add("UpdatedDateTime", DateTime.Now, DbType.DateTime);

var data = _dapperHelper.Update<string>("[dbo].[SP_Update_Job]",

dbPara,

commandType: CommandType.StoredProcedure);

return data;

}

}

}

Add Connection String

Add a connection string into the appsettings.json file. Here is the code of the appsettings.json file.

{

"ConnectionStrings": {

"DefaultConnection": "data source=TheCodeHubs-PC;initial catalog=CoreMaster;User Id=sa;Password=******;"

},

"Logging": {

"LogLevel": {

"Default": "Warning",

"Microsoft.Hosting.Lifetime": "Information"

}

},

"AllowedHosts": "*"

}

Changes in Startup class

Now, we need to add a connection string context and register our services.

Code to add connection string –

services.AddDbContext<DataContext>(options =>

options.UseSqlServer(

Configuration.GetConnectionString("DefaultConnection")));

Add a service to scoped.

//Job service

services.AddScoped<IJobService, JobService>();

//Register dapper in scope

services.AddScoped<IDapperHelper, DapperHelper>();

Add Controller

It’s now time to add a Job Controller inside the Controller folder. Below is the full code of JobController.

 

using System;

using System.Collections.Generic;

using System.IO;

using System.Linq;

using System.Net.Http.Headers;

using System.Threading.Tasks;

using CoreDemo_3_0.Entities;

using CoreDemo_3_0.Interfaces;

using CoreDemo_3_0.Models;

using Microsoft.AspNetCore.Hosting;

using Microsoft.AspNetCore.Mvc;

namespace CoreDemo_3_0.Controllers

{

public class JobController : Controller

{

private readonly IJobService _jobManager;

private readonly IHostingEnvironment _hostingEnvironment;

public JobController(IJobService jobManager, IHostingEnvironment hostingEnvironment)

{

_jobManager = jobManager;

_hostingEnvironment = hostingEnvironment;

}

public ActionResult Index()

{

var data = _jobManager.ListAll();

string baseUrl = $"{this.Request.Scheme}://{this.Request.Host}{this.Request.PathBase}";

foreach (var item in data)

{

if (!string.IsNullOrEmpty(item.JobImage))

item.JobImage = Path.Combine(baseUrl, "Images", item.JobImage);

else

item.JobImage = Path.Combine(baseUrl, "Images", "404.png");

}

return View(data);

}

#region Add Job

public ActionResult Add()

{

return View("Form", new JobModel());

}

[HttpPost]

[ValidateAntiForgeryToken]

public ActionResult Add(JobModel model)

{

if (ModelState.IsValid)

{

var fileName = "";

if (Request.Form.Files.Count > 0)

{

var file = Request.Form.Files[0];

var webRootPath = _hostingEnvironment.WebRootPath;

var newPath = Path.Combine(webRootPath, "images");

if (!Directory.Exists(newPath)) Directory.CreateDirectory(newPath);

if (file.Length > 0)

{

fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');

var fullPath = Path.Combine(newPath, fileName);

using (var stream = new FileStream(fullPath, FileMode.Create))

{

file.CopyTo(stream);

}

}

}

var job = new Job()

{

CityId = model.CityId,

JobImage = fileName,

CreatedBY = "1",

CreatedDateTime = DateTime.Now,

JobTitle = model.JobTitle,

UpdatedBY = "1",

IsActive = model.IsActive,

UpdatedDateTime = DateTime.Now

};

_jobManager.Create(job);

return RedirectToAction("Index", "Job");

}

return View("Form", model);

}

#endregion

#region Edit Job

public ActionResult Edit(int JobId)

{

var jobEntity = _jobManager.GetByJobId(JobId);

var jobModel = new JobModel

{

JobID = jobEntity.JobID,

CityId = jobEntity.CityId,

JobImage = jobEntity.JobImage,

CreatedBY = jobEntity.CreatedBY,

CreatedDateTime = jobEntity.CreatedDateTime,

JobTitle = jobEntity.JobTitle,

UpdatedBY = jobEntity.UpdatedBY,

IsActive = jobEntity.IsActive,

UpdatedDateTime = jobEntity.UpdatedDateTime

};

return View("Form", jobModel);

}

[HttpPost]

[ValidateAntiForgeryToken]

public ActionResult Edit(JobModel model)

{

if (ModelState.IsValid)

{

var fileName = model.JobImage ?? "";

if (Request.Form.Files.Count > 0)

{

var file = Request.Form.Files[0];

var webRootPath = _hostingEnvironment.WebRootPath;

var newPath = Path.Combine(webRootPath, "images");

if (!Directory.Exists(newPath)) Directory.CreateDirectory(newPath);

if (file.Length > 0)

{

fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');

var fullPath = Path.Combine(newPath, fileName);

using (var stream = new FileStream(fullPath, FileMode.Create))

{

file.CopyTo(stream);

}

}

}

var job = new Job()

{

JobID = model.JobID,

CityId = model.CityId,

JobImage = fileName,

JobTitle = model.JobTitle,

UpdatedBY = "1",

IsActive = model.IsActive,

};

_jobManager.Update(job);

return RedirectToAction("Index", "Job");

}

return View("Form", model);

}

#endregion

#region Delete Job

public ActionResult Delete(int JobId)

{

var jobEntity = _jobManager.Delete(JobId);

return RedirectToAction("Index", "Job");

}

#endregion

}

}

Add Views

It is time to add a ViewController inside Views > Job folder. Below is the code for all views of JobController.

 

Form.cshtml

@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

@model CoreDemo_3_0.Models.JobModel

@{

ViewData["Title"] = "Add Job";

Layout = "~/Views/Shared/_Layout.cshtml";

}

<div class="row">

<!-- left column -->

<div class="col-md-12">

<!-- general form elements -->

<div class="box box-primary">

<!-- form start -->

@using (Html.BeginForm(Model.JobID == 0 ? "Add" : "Edit", "Job", FormMethod.Post, new { enctype = "multipart/form-data" }))

{

<div class="box-body">

@Html.AntiForgeryToken()

<div class="row">

@Html.HiddenFor(model => model.JobID)

<div class="col-xs-12" f>

<label>Job Title</label>

@Html.TextBoxFor(model => model.JobTitle, new { @class = "form-control", @placeholder = "Job Title" })

</div>

</div>

<br />

<div class="row">

<div class="col-xs-12">

<label>Enter City ID</label>

@Html.TextBoxFor(model => model.CityId, new { @class = "form-control" })

</div>

</div>

<br />

<div class="row">

<div class="col-xs-12">

<label>Job Image</label>

<input type="file" id="file" name="file">

@Html.HiddenFor(model => model.JobImage)

</div>

</div>

<div class="row">

<div class="col-xs-12">

@Html.CheckBoxFor(model => model.IsActive, new { @class = "col-form-checkbox" }) Vacancy Available

</div>

</div>

</div><!-- /.box-body -->

<div class="box-footer">

<button type="submit" class="btn btn-primary"><i class="fa fa-save"></i> Save</button>

</div>

}

</div>

</div>

</div>

Index.cshtml

@model List<CoreDemo_3_0.Entities.Job>

@{

ViewData["Title"] = "Job List";

}

<a href="/Job/Add" class="btn btn-primary">Add</a>

<br />

<table id="_DataTable" class="table compact table-striped table-bordered nowrap dataTable" aria-describedby="_DataTable_info">

<thead>

<tr role="row">

<th class="sorting_asc" role="columnheader" tabindex="0" aria-controls="_DataTable" rowspan="1" colspan="1" aria-sort="ascending" aria-label="Image: activate to sort column descending" style="width: 127px;">Image</th>

<th class="sorting" role="columnheader" tabindex="0" aria-controls="_DataTable" rowspan="1" colspan="1" aria-label="Title: activate to sort column ascending" style="width: 209px;">Title</th>

<th class="sorting" role="columnheader" tabindex="0" aria-controls="_DataTable" rowspan="1" colspan="1" aria-label="City: activate to sort column ascending" style="width: 116px;">City</th>

<th class="sorting" role="columnheader" tabindex="0" aria-controls="_DataTable" rowspan="1" colspan="1" aria-label="Vacancy: activate to sort column ascending" style="width: 127px;">Vacancy</th>

<th class="sorting" role="columnheader" tabindex="0" aria-controls="_DataTable" rowspan="1" colspan="1" aria-label="Created Date: activate to sort column ascending" style="width: 190px;">Created Date</th>

<th style="width: 38px;" class="sorting" role="columnheader" tabindex="0" aria-controls="_DataTable" rowspan="1" colspan="1" aria-label=" Action : activate to sort column ascending"> Action </th>

</tr>

</thead>

<tbody role="alert" aria-live="polite" aria-relevant="all">

@foreach (var item in Model)

{

<tr class="even">

<td style="text-align:left"><img src="@item.JobImage" alt="Image" width="50" height="50"></td>

<td style="text-align:left">@item.JobTitle</td>

<td style="text-align:left">@item.CityId</td>

<td style="text-align:left">@(item.IsActive == true ? "Yes" : "No")</td>

<td style="text-align:right">@item.CreatedDateTime</td>

<td class="text-center ">

<a href="/Job/Edit?JobID=@item.JobID" title="Edit">Edit <i class="fa fa-edit"></i></a><a href="/Job/Delete?&JobID=@item.JobID" class="" onclick="return confirm(" Are you sure you want to delete this job?");" title="Delete">Delete <i class="fa fa-times"></i></a>

</td>

</tr>

}

</tbody>

</table>

Output

1.    1.Index Page

2. Add Page

3. Edit Page



Bottom of Form