1
Answer

How to use save button to make save in both cases insert and

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
  1. public class EmployeesController : Controller  
  2. {  
  3. private readonly IEmployees _context;  
  4. public EmployeesController(IEmployees context)  
  5. {  
  6. _context = context;  
  7. }  
  8. [HttpPost]  
  9. public IActionResult Create()  
  10. {  
  11. return View();  
  12. }  
create view
  1. <form asp-action="Create">  
  2. <div class="form-group">  
  3. <label asp-for="EmployeeId" class="control-label"></label>  
  4. <input asp-for="EmployeeId" class="form-control" />  
  5. <span asp-validation-for="EmployeeId" class="text-danger"></span>  
  6. </div>  
  7. <div class="form-group">  
  8. <label asp-for="BranchCode" class="control-label"></label>  
  9. <input asp-for="BranchCode" class="form-control" />  
  10. <span asp-validation-for="BranchCode" class="text-danger"></span>  
  11. </div>  
  12. <div class="form-group">  
  13. <label asp-for="EmployeeName" class="control-label"></label>  
  14. <input asp-for="EmployeeName" class="form-control" />  
  15. <span asp-validation-for="EmployeeName" class="text-danger"></span>  
  16. </div>  
  17. <button id="BtnSave" style="display:inline">Save</button>  
  18. </form>  
  19. </div>  
  20. </div>  
  1. namespace TAB.Data.InfraStructure  
  2. {  
  3. public class EFRepository<T> : IRepository<T> where T : class  
  4. {  
  5. protected TabDbContext _context { getset; }  
  6. public EFRepository(TabDbContext context)  
  7. {  
  8. _context = context;  
  9. }  
  10. public virtual T Add(T t)  
  11. {  
  12. _context.Set<T>().Add(t);  
  13. _context.SaveChanges();  
  14. return t;  
  15. }  
  16. public virtual async Task<T> AddAsyn(T t)  
  17. {  
  18. _context.Set<T>().Add(t);  
  19. await _context.SaveChangesAsync();  
  20. return t;  
  21. }  
  22. public virtual T Find(Expression<Func<T, bool>> match)  
  23. {  
  24. return _context.Set<T>().SingleOrDefault(match);  
  25. }  
  26. public virtual async Task<T> FindAsync(Expression<Func<T, bool>> match)  
  27. {  
  28. return await _context.Set<T>().SingleOrDefaultAsync(match);  
  29. }  
  30. public ICollection<T> FindAll(Expression<Func<T, bool>> match)  
  31. {  
  32. return _context.Set<T>().Where(match).ToList();  
  33. }  
  34. public async Task<ICollection<T>> FindAllAsync(Expression<Func<T, bool>> match)  
  35. {  
  36. return await _context.Set<T>().Where(match).ToListAsync();  
  37. }  
  38. public virtual T Update(T t, object key)  
  39. {  
  40. if (t == null)  
  41. return null;  
  42. T exist = _context.Set<T>().Find(key);  
  43. if (exist != null)  
  44. {  
  45. _context.Entry(exist).CurrentValues.SetValues(t);  
  46. _context.SaveChanges();  
  47. }  
  48. return exist;  
  49. }  
  50. public virtual async Task<T> UpdateAsyn(T t, object key)  
  51. {  
  52. if (t == null)  
  53. return null;  
  54. T exist = await _context.Set<T>().FindAsync(key);  
  55. if (exist != null)  
  56. {  
  57. _context.Entry(exist).CurrentValues.SetValues(t);  
  58. await _context.SaveChangesAsync();  
  59. }  
  60. return exist;  
  61. }  
  62. public virtual void Save()  
  63. {  
  64. _context.SaveChanges();  
  65. }  
  66. public async virtual Task<int> SaveAsync()  
  67. {  
  68. return await _context.SaveChangesAsync();  
  69. }  
  70. public virtual IQueryable<T> FindBy(Expression<Func<T, bool>> predicate)  
  71. {  
  72. IQueryable<T> query = _context.Set<T>().Where(predicate);  
  73. return query;  
  74. }  
Answers (1)