LinqToSql MVC3 更新

发布于 2024-11-02 03:26:17 字数 534 浏览 1 评论 0原文

我使用 ASP.NET MVC3 并作为数据层 LinqToSql。 我有点困惑如何编辑实体。

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();
}

Edit() 中的变量product 是可以的,但是编辑后查看在 [HttpPost] Edit 中传递的变量 链接属性为 null,并且似乎与我的 DataContext 分离。 我还应该在 EditProduct 方法中执行什么代码来更新实体?

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

未蓝澄海的烟 2024-11-09 03:26:17

我假设在您的存储库中您有一个用于数据上下文的对象。在您的 EditProduct 调用中,您应该有类似的内容:

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

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

您还可以附加传入的产品并保存(如果您有时间戳列):

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

如果您没有时间戳列,则 L2S 将抛出有关无法检查的错误这是状态。

如果实体声明版本成员或没有更新检查策略,则只能以修改后的状态(没有原始状态)进行附加。

如果您向数据库添加时间戳列,则 L2S 可以执行上述操作。

这是一个更深入的解释。

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.

來不及說愛妳 2024-11-09 03:26:17
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. 您的 Edit(Product 产品) 方法将从 HTTP 请求参数创建一个 Product 实例。它使用反射并查看 Product 类的属性来查看与 HTTP 请求匹配的内容来完成此操作。这称为模型绑定,您可以进一步研究它以了解它的工作原理。您的产品实例没有链接并且与上下文分离的原因是因为它是作为新的普通对象创建的。
  2. 您的 EditProduct 代码可能类似于上面的内容。

(留给读者作为练习,让上面的代码处理异常、验证等)

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.)

洋洋洒洒 2024-11-09 03:26:17

我找到了最适合我需求的方法

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);
}

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文