s

ASP.NET CORE 5.0 MVC CURD operations using List edit button Edit

author
Rakesh Kumar Sutar | calendar 04 February 2021 | 1830

What is CURD operations

CURD operation is the data manipulation method which includes create, update, read, Delete. We will use the MVC design pattern to create CURD operation in ASP.NET MVC 5.0.

Country.cs

This is the model class. In this class we are going to create three class one is CountryData which will create a instance of Country data i:e _countries, Second is two partial class Country. In the first partial class country, we have declare the three items of Country List with get and set method i:e Id, Name and Code. In the second partial class country, we have declared five methods to handle the Country list.
1.public List TestData() - This method will add data to the CountryData._countries and also return CountryData._countries.
2.public List Get() - This method will return CountryData._countries, when Controller call this method.
3.public bool Insert(Country country) - This method will return true if a new instance of Country is added to the CountryData._countries.
4.public bool Update(int Id, Country country) - This method will return true, when CountryData._countries item update some data of List.
5. public bool Delete(int Id) - This method will return true if a particular item is removed from list, otherwise it will return false.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace CountryList.Models
{
    public static class CountryData
    {
        public static List _countries = new List();
    }
    public partial class Country
    {
        public int Id { get; set; }
        public String Name { get; set; }
        public String Code { get; set; }

    }

    public partial class Country
    {
        public List Get()
        {
            if(CountryData._countries.Count==0)
            {
                TestData();
            }
            
            return CountryData._countries;
        }


        public bool Insert(Country country)
        {
            CountryData._countries.Add(country);
            return true;
        }

        public bool Update(int Id, Country country)
        {
            var select = CountryData._countries.Where(x => x.Id == Id).FirstOrDefault();
            if (select != null)
            {
                select.Code = country.Code;
                select.Name = country.Name;
            }
            return true;
        }

        public bool Delete(int Id)
        {
            var country = CountryData._countries.Where(x => x.Id == Id).FirstOrDefault();
            if (country != null)
            {
                CountryData._countries.Remove(country);
                return true;
            }
            return false;
        }

       

        public List TestData()
        {
            CountryData._countries.Add(new Country() { Id = 1, Name = "India", Code = "91" });
            CountryData._countries.Add(new Country() { Id = 2, Name = "USA", Code = "1" });
            CountryData._countries.Add(new Country() { Id = 3, Name = "Russia", Code = "96" });
            return CountryData._countries;
        }
    }

}

HelloController.cs

RightClick on the Controller Folder in Solution Explorer->Click on Add-> Select Controller-> Select MVC Controller with read/write actions-> Click on ADD-> Change Name to HelloController.cs->Click on ADD. It will automatically generate Controller class.

using CountryList.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace CountryList.Controllers
{
    public class HelloController : Controller
    {
        // GET: HelloController
        public ActionResult Index()
        {
            Country country = new Country();
            return View(country.Get());
        }

        // GET: HelloController/Details/5
        public ActionResult Details(int id)
        {
          
            return View();
        }
       

        // GET: HelloController/Create
        public ActionResult Create()
        {
            
            return View();
        }

        // POST: HelloController/Create
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(IFormCollection collection)
        {
           
            Country newCountry = new Country {Id=CountryData._countries.Count() 1,Code=collection["Code"],Name=collection["Name"] };
            new Country().Insert(newCountry);
            try
            {
                return RedirectToAction(nameof(Index));
            }
            catch
            {
                return View();
            }
        }

        // GET: HelloController/Edit/5
        public ActionResult Edit(int id)
        {
            return View();
        }

        // POST: HelloController/Edit/5
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(int id, IFormCollection collection)
        {
            Country newCountry = new Country { Id = id, Code = collection["Code"], Name = collection["Name"] };
            new Country().Update(id, newCountry);
            try
            {
                return RedirectToAction(nameof(Index));
            }
            catch
            {
                return View();
            }
        }

        // GET: HelloController/Delete/5
        public ActionResult Delete(int id)
        {
           
            return View();
        }

        // POST: HelloController/Delete/5
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Delete(int id, IFormCollection collection)
        {

            new Country().Delete(id);
            try
            {
                return RedirectToAction(nameof(Index));
            }
            catch
            {
                return View();
            }
        }
    }
}

View

RightClick on each method in HelloController.cs -> Click on AddView -> Select Razor View -> Click on ADD.
It will automatically generate view for each method.

Output