如何在MVC3中保留编辑?

时间:2011-08-24 14:44:47

标签: asp.net-mvc-3 entity-framework-4 edit

public ActionResult Edit(int id)
{
    var productBrand = brandRepo.FindProductBrand(id);
    ProductBrandModel model = Mapper.Map<ProductBrand, ProductBrandModel>(productBrand);
    return View(model);
}

[HttpPost]
public ActionResult Edit(ProductBrandModel model)
{
    if (ModelState.IsValid)
    {
        var productBrand = brandRepo.FindProductBrand(model.BrandId);
        productBrand.Name = model.Name;               
        //How to persist that information?
    }
}

我有一个EF生成类ProductBrand和一个名为ProductBrandModel的视图模型。

如何使用Entity Framework保留编辑信息?我的brandRepo应该有一个名为SaveChanges的void方法,其中我会去:

public void SaveChanges()
{
    dbEntities.SaveChanges();
}

2 个答案:

答案 0 :(得分:1)

正如您所假设的,您必须使用.SaveChanges()方法将更改提交到数据库。在您的情况下,brandRepo.SaveChanges()会委托给dbEntities.SaveChanges()

作为旁注:在简单的情况下,单独的存储库类仅引入复杂性而不提供任何好处。实体框架的DbContext非常类似于简单的存储库本身,因此您不需要一个存储库。

当然,为了可测试性,间接层可能有意义。

如果没有存储库,您的代码可能看起来像这样:

public ActionResult Edit(int id)
{
    var productBrand = dbEntities.ProductBrands.Find(x => x.BrandId = id);
    ProductBrandModel model = Mapper.Map<ProductBrand, ProductBrandModel>(productBrand);
    return View(model);
}

[HttpPost]
public ActionResult Edit(ProductBrandModel model)
{
    if (ModelState.IsValid)
    {
        var productBrand = dbEntities.ProductBrands.Find(x => x.BrandId = id);
        // or something similar, I don't know the inner workings of your
        // brandRepo.FindProductBrand(id)

        productBrand.Name = model.Name;               
        dbEntities.SaveChanges();
    }
}

答案 1 :(得分:0)

我喜欢在我的存储库中有一个save方法,以及我从网上获得的实体框架帮助方法。 SaveCustomer是我的存储库类方法,下面是帮助程序类。在您的情况下,您可以将模型传递给

brandRepository.SaveProdctBrand(productBrand)

(有助于拼出好的命名约定和fxcop规则的名称)

public void SaveCustomer(Customer customer)
{
  using (var ctx = new WebStoreEntities())
  {
    if (customer.CustomerId > 0)
    {
        //It's an existing record, update it.
        ctx.Customers.AttachAsModified(customer);
        ctx.SaveChanges();
    }
    else
    {
        //its a new record.
        ctx.Customers.AddObject(customer);
        ctx.SaveChanges();
    }
  }
}

助手类如下

public static class EntityFrameworkExtensions
{
/// <summary>
/// This class allows you to attach an entity.
/// For instance, a controller method Edit(Customer customer)
/// using ctx.AttachAsModified(customer); 
/// ctx.SaveChanges();
/// allows you to easily reattach this item for udpating.
/// Credit goes to: http://geekswithblogs.net/michelotti/archive/2009/11/27/attaching-modified-entities-in-ef-4.aspx
/// </summary>
public static void AttachAsModified<T>(this ObjectSet<T> objectSet, T entity) where T : class
{
    objectSet.Attach(entity);
    objectSet.Context.ObjectStateManager.ChangeObjectState(entity, EntityState.Modified);
}

/// <summary>
/// This marks an item for deletion, but does not currently mark child objects (relationships).
/// For those cases you must query the object, include the relationships, and then delete.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="objectSet"></param>
/// <param name="entity"></param>
public static void AttachAsDeleted<T>(this ObjectSet<T> objectSet, T entity) where T : class
{
    objectSet.Attach(entity);
    objectSet.Context.ObjectStateManager.ChangeObjectState(entity, EntityState.Deleted);
}

public static void AttachAllAsModified<T>(this ObjectSet<T> objectSet, IEnumerable<T> entities) where T : class
{
    foreach (var item in entities)
    {
        objectSet.Attach(item);
        objectSet.Context.ObjectStateManager.ChangeObjectState(item, EntityState.Modified);
    }
}
}