I am trying to apply the method below on a Telerik Grid (AJAX calls). Here is how I used to update a row in a database:
REPOSITORY:
===========
public class GenericRepository<TEntity> where TEntity : class
{
internal UdsContext context;
internal DbSet<TEntity> dbSet;
public GenericRepository(UdsContext context)
{
this.context = context;
this.dbSet = context.Set<TEntity>();
}
public virtual void Update(TEntity entityToUpdate)
{
dbSet.Attach(entityToUpdate);
context.Entry(entityToUpdate).State = EntityState.Modified;
}
...
public class CourseRepository : GenericRepository<Course>
{
public CourseRepository(SchoolContext context) : base(context)
{
}
...
CONTROLLER:
===========
public ActionResult Edit(int id)
{
Course course = unitOfWork.CourseRepository.GetByID(id);
return View(course);
}
[HttpPost]
public ActionResult Edit(Course course)
{
try
{
if (ModelState.IsValid)
{
unitOfWork.CourseRepository.Update(course);
unitOfWork.Save();
return RedirectToAction("Index");
}
}
catch (DataException)
{
ModelState.AddModelError("", "Unable to save changes.");
}
return View(course);
}
However, on the Telegrid examples they use the following:
public static EditableProduct One(Func<EditableProduct, bool> predicate)
{
return All().Where(predicate).FirstOrDefault();
}
public static void Update(EditableProduct product)
{
EditableProduct target = One(p => p.ProductID == product.ProductID);
if (target != null)
{
target.ProductName = product.ProductName;
target.UnitPrice = product.UnitPrice;
target.UnitsInStock = product.UnitsInStock;
target.Discontinued = product.Discontinued;
target.LastSupply = product.LastSupply;
}
}
So their action accepts an ID and mine accepts the entity object.
I am trying to find out how I can adapt the current Telerik sample (AJAX client binding) with the way I do updates in my generic Repository.
Sorry for the long post.
Thank you