开发者

LinqToSql MVC3 Update

开发者 https://www.devze.com 2023-02-26 05:55 出处:网络
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)

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开发者_Go百科);
}


[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:

Product prod = dataContext.Products.Single(p=>p.ProductID == product.ProductID);

prod.PropertyA = product.PropertyA;
prod.PropertyB = product.PropertyB;
dataContext.SubmitChanges();

You can also attach the product coming in and save (if you have a timestamp column):

dataContext.Products.Attach(product,true);
dataContext.SubmitChanges();

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.


public void EditProduct(Product product) {
    using (var context = new MyContext()) {
        var dbProduct = context.Product.Single(r => r.Id == product.Id);
        dbProduct.Property = product.Property;
        dbProduct.ProductCategory = context.ProductCategory.Single(r => r.Id == product.ProductCategoryId);
        context.SubmitChanges();
    }
}
  1. Your Edit(Product product) method will create a Product instance from HTTP request parameters. It does this using reflection and looking through the Product class's properties to see what matches the HTTP request. This is called model binding and you can look into it more to learn how it works. The reason your product instance doesn't have links and is detached from your context is because it was created as a new plain object.
  2. Your EditProduct code could be something like the above.

(it's left as an exercise for the reader to make the code above handle exceptions, validation, etc.)


I have found method that best fits my needs

public ActionResult Edit(int id)
{
    ViewBag.Categories = _repository.GetAllCategories();
    var product = _repository.GetById(id);
    return View(product);
}


[HttpPost]
public ActionResult Edit(int id, FormCollection collection) {
    var product = _repository.GetById(id);
    if (TryUpdateModel(product)) {
        _repository.Commit();
        return RedirectToAction("Index");
    }
    ViewBag.Categories = _repository.GetAllCategories();
    return View(product);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号