Click here to Skip to main content
15,890,825 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Problem

How to use save button in Create Action to make save in both cases insert and update using repository pattern ?

and what changes i will make in view to accept update and insert .

what i writing is when make new record or update record then use save button it will save using create action in employee controller using HTTP post

I use Repository Pattern generics in visual studio 2017 asp.net core 2.1 with sql server 2012

What I have tried:

<pre lang="c#">public class EmployeesController : Controller
    {
        private readonly IEmployees _context;

        public EmployeesController(IEmployees context)
        {
            _context = context;
        }

        [HttpPost]
        public IActionResult Create()
        {
How to use Create Action for insert and update using repository pattern 
            return View();
        }
create view
<form asp-action="Create">

            
            <div class="form-group">
                <label asp-for="EmployeeId" class="control-label"></label>
                <input asp-for="EmployeeId" class="form-control" />
                <span asp-validation-for="EmployeeId" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="BranchCode" class="control-label"></label>
                <input asp-for="BranchCode" class="form-control" />
                <span asp-validation-for="BranchCode" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="EmployeeName" class="control-label"></label>
                <input asp-for="EmployeeName" class="form-control" />
                <span asp-validation-for="EmployeeName" class="text-danger"></span>
            </div>
            <button id="BtnSave" style="display:inline">Save</button>
        </form>
    </div>
</div>


namespace TAB.Data.InfraStructure
{
 
    public class EFRepository<T> : IRepository<T> where T : class
    {
        protected TabDbContext _context { get; set; }
        public EFRepository(TabDbContext context)
        {
            _context = context;
        }
       
     

        public virtual T Add(T t)
        {

            _context.Set<T>().Add(t);
            _context.SaveChanges();
            return t;
        }

        public virtual async Task<T> AddAsyn(T t)
        {
            _context.Set<T>().Add(t);
            await _context.SaveChangesAsync();
            return t;

        }

        public virtual T Find(Expression<Func<T, bool>> match)
        {
            return _context.Set<T>().SingleOrDefault(match);
        }

        public virtual async Task<T> FindAsync(Expression<Func<T, bool>> match)
        {
            return await _context.Set<T>().SingleOrDefaultAsync(match);
        }

        public ICollection<T> FindAll(Expression<Func<T, bool>> match)
        {
            return _context.Set<T>().Where(match).ToList();
        }

        public async Task<ICollection<T>> FindAllAsync(Expression<Func<T, bool>> match)
        {
            return await _context.Set<T>().Where(match).ToListAsync();
        }

      
        public virtual T Update(T t, object key)
        {
            if (t == null)
                return null;
            T exist = _context.Set<T>().Find(key);
            if (exist != null)
            {
                _context.Entry(exist).CurrentValues.SetValues(t);
                _context.SaveChanges();
            }
            return exist;
        }

        public virtual async Task<T> UpdateAsyn(T t, object key)
        {
            if (t == null)
                return null;
            T exist = await _context.Set<T>().FindAsync(key);
            if (exist != null)
            {
                _context.Entry(exist).CurrentValues.SetValues(t);
                await _context.SaveChangesAsync();
            }
            return exist;
        }



        public virtual void Save()
        {

            _context.SaveChanges();
        }

        public async virtual Task<int> SaveAsync()
        {
            return await _context.SaveChangesAsync();
        }

        public virtual IQueryable<T> FindBy(Expression<Func<T, bool>> predicate)
        {
            IQueryable<T> query = _context.Set<T>().Where(predicate);
            return query;
        }
Posted
Updated 7-Jan-19 20:10pm

1 solution

Hi,

in view,you have to add one hidden field which is primary field of database.eg:ID

In get action method i.e. for rendering view,you have to get data from database and render to view so that you will get hidden field ID also there.

so in controller action post method,you have to check if that ID is present or not.

If that ID is present that means you are updating that data.so call update method of Entity Framework.and if ID is not present Insert data.

The best way to update is first get records from the database by using respository pattern Find method and if found update it with EntityState as modified.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900