我的问题是当我尝试使用两个 Html.RenderAction 渲染视图时。它说:“操作无法完成,因为 DbContext 已被释放”。
我以这种方式配置了 Ninject:
Bind<IUnitOfWork>().To<UnitOfWork>().InRequestScope;
但如果我以默认方式...
Bind<IUnitOfWork>().To<UnitOfWork>()
没有错误。
我必须在 RequestScope 中使用它(所以我认为),但我该怎么做呢?似乎当第二个 Html.RenderAction 被称为前一个 DbContext 时,它就被释放了!
更新:
这是主要视图(为简洁起见)
@model FoodAway.Model.Product
@Html.ValidationSummary(true)
<fieldset>
<legend>Producto</legend>
@using (Html.BeginForm())
{
<div class="editor-label">
@Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
@Html.HiddenFor(model => model.Id)
<p>
<input type="submit" value="Guardar" />
</p>
}
<fieldset>
<div class="editor-label">
@Html.LabelFor(model => model.Ingredients)
</div>
<div class="editor-field">
@{Html.RenderAction("IngredientsToRemoveList", "Ingredients");}
</div>
</fieldset>
<fieldset>
@{Html.RenderAction("IngredientsToAddList", "Ingredients");}
</fieldset>
</fieldset>
和他的控制器/动作:
public ActionResult EditProduct(string name)
{
Product product = unitOfWork.ProductRepository.Get(i => i.Name ==name).FirstOrDefault();
if (product == null)
return HttpNotFound();
return View(product);
}
所以,DBContext 中的错误是当我有这 2 个 RenderAction 方法时,奇怪的是如果我只有 1 个 RenderAction 没有问题!!!!!!