I use ASP.NET MVC3 and as Data layer LinqToSql.
I`m a little bit confuse how can i edit an entity.
public ActionResult Edit(int id)
{
var product = _repository.GetById(id);
return View(product);
}
[HttpPost]
public ActionResult Edit(Product product)
{
if (ModelState.IsValid)
{
_repository.EditProduct(product);
return RedirectToAction("Index");
}
return View();
}
Variable product in Edit() is ok, but after editing view the variable passed in [HttpPost] Edit
has null in link properties and seems to be detached from my DataContext.
And also what code should i execute in EditProduct method to update entity?
Thanks.
I am assuming in your repository you have an object for your data context. In your EditProduct call you should have something like:
You can also attach the product coming in and save (if you have a timestamp column):
If you don’t have a timestamp column then L2S will throw an error about not being able to check it’s state.
An entity can only be attached as modified without original state if it declares a version member or does not have an update check policy.
If you add a timestamp column to your DB then L2S can do the above.
Here’s a deeper explanation.