Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Sunday, January 6, 2019 8:40 PM
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
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"><b>Save</b></button>
</form>
</div>
</div>
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
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 int Count()
{
return _context.Set<T>().Count();
}
public async Task<int> CountAsync()
{
return await _context.Set<T>().CountAsync();
}
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;
}
private bool disposed = false;
protected virtual void Dispose(bool disposing)
{
if (!this.disposed)
{
if (disposing)
{
_context.Dispose();
}
this.disposed = true;
}
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
All replies (4)
Monday, January 7, 2019 8:50 AM ✅Answered
Hi engahmed-barbary ,
If you want to make new record or update record using the same create action in the controller , you need to use a key to check if this data exists.
A simple demo like below
public async Task<IActionResult> Create(Employee employee)
{
if (ModelState.IsValid)
{
if (employee.EmployeeId!= 0)
{
try
{
//update Employee table using repository pattern you want
}
catch (DbUpdateConcurrencyException)
{
if (!EmployeeExists(employee.EmployeeId))
{
return NotFound();
}
else
{ throw; }
}
return RedirectToAction(nameof(Index));
}
//Add Employee table using repository pattern you want
return RedirectToAction(nameof(Index));
}
return View();
}
Best Regards ,
Sherry
Sunday, January 6, 2019 9:24 PM
its pretty simple, in the posy action of create, use the posted key to check if it exists, if so update, else insert. unless the key is a guid, you should encrypt the key. otherwise hacking is pretty simple, though exposing the key in any form makes hacking simpler.
Sunday, January 6, 2019 11:07 PM
can you show to me answer if possible
Sunday, January 6, 2019 11:17 PM
can you show to me answer if possible
As suggested in your other threads, go through the Getting Started tutorials to learn MVC basics. The current problem you are facing has to do with model binding and building the UI not the repository.
Please go through the tutorials. That what the tutorials are for...