TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
ahmed elbarbary
NA
1.6k
277.8k
How to use save button to make save in both cases insert and
Jan 6 2019 5:59 PM
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()
{
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;
}
Reply
Answers (
1
)
How to add functionality to buttons( Next previous first las
CRUD Operation Using Stored Procedures in asp.net c#