我有2个类,并尝试在类的视图中使用引用到另一个类给出错误:
ObjectContext实例已被释放,不能再用于需要连接的操作。
班级“CLIENTE”
public class Cliente : IEntidadeBase
{
[Key]
[Display(Name = "Cód. de Cliente")]
public int ClienteID { get; set; }
[Display(Name = "Cód. de Pessoa")]
public int PessoaID { get; set; }
public string Matriz { get; set; }
[Display(Name = "Limite de crédito")]
public decimal LimiteCredito { get; set; }
[ForeignKey("PessoaID")]
public virtual Pessoa Pessoa { get; set; }
}
班级“PESSOA”
public class Pessoa : IEntidadeBase
{
[Key]
public int PessoaID { get; set; }
public int? EmpresaIDVinc { get; set; }
[Display(Name = "Matrícula")]
public string Matricula { get; set; }
[Display(Name = "Nome")]
[Obrigatorio]
public string Nome { get; set; }
}
控制器视图更改:
[Authorize]
[ControleDeAcesso(TipoAcao.Normal)]
public ActionResult Detalhar(int id)
{
using (var db = new ERPContext())
{
Cliente cliente = db.Cliente.Find(id);
var retorno = FlexGestor.Helpers.EntidadeBaseExt.ValidarRegistro(cliente, TipoAcao.Visualizar);
if (retorno != "")
{
TempData["MsgRetornoError"] = retorno;
return RedirectToAction("Index", "Home");
}
return View(cliente);
}
}
查看代码:
@model FlexGestor.Models.Cliente
@Html.TituloPagina("Visualizando Cliente")
@using (Html.BeginForm())
{
@Html.ValidationSummary(true);
<div class="linha left">
@Html.HiddenFor(m => m.ClienteID)
@Html.LabelFor(m => m.Pessoa.Nome) @Html.ValidationMessageFor(m => m.Pessoa.Nome)<br />
@Html.TextBoxFor(m => m.Pessoa.Nome, new { style = "width:250px;" })<br />
@Html.LabelFor(m => m.LimiteCredito) @Html.ValidationMessageFor(m => m.LimiteCredito)<br />
@Html.TextBoxFor(m => m.LimiteCredito, new { style = "width:250px;" })<br />
@Html.BotaoTelaDetalhar()
</div>
}
<div class="linha rodape"></div>
答案 0 :(得分:1)
我不是100%确定这是否可以解决您的问题,但请尝试更改此问题并查看是否有帮助
[Authorize]
[ControleDeAcesso(TipoAcao.Normal)]
public ActionResult Detalhar(int id)
{
Cliente cliente = null;
using (var db = new ERPContext())
{
cliente = db.Cliente.Find(id);
var retorno = FlexGestor.Helpers.EntidadeBaseExt.ValidarRegistro(cliente, TipoAcao.Visualizar);
if (retorno != "")
{
TempData["MsgRetornoError"] = retorno;
return RedirectToAction("Index", "Home");
}
}
if(cliente != null && cliente.pessoa != null)
{
return View(cliente);
}
else
{
// do something else here as the view does not have required stuff
}
}