As an example, below is the example of the Entity, Interface, Repository, and MVC System Controller.
Following the code of formulário webform.aspx
I use to display Pedido usando o ReportViewer
, the problem is that when I try to display the request it always returns Null
.
When debugging the system nor arriving at Repositorio
, the impression I have is that the way I'm doing it works when it's a Controller and fails when it's a WebForm.
With this Entity, Interface and Repository structure, how do I populate the ReportDataSource()
object in a WebForm form?
REQUEST ENTITY:
public class Pedido
{
[Key]
public int PedidoID { get; set; }
public DateTime? DataPedido { get; set; }
}
INTERFACE:
public interface IPedido: IDisposable
{
Pedido ObterPedidoPorID(int pPedidoID);
}
REPOSITORY:
public class PedidoRepositorio: IPedido
{
private readonly ctx _repositorio = new ctx();
private bool disposed = false;
public Pedido ObterPedidoPorID(int pPedidoID)
{
return _repositorio.Pedidos.FirstOrDefault(c => c.PedidoID == pPedidoID);
}
protected virtual void Dispose(bool disposing)
{
if (!this.disposed)
{
if (disposing)
{
_repositorio.Dispose();
}
}
this.disposed = true;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}
CONTROLLER:
public class PedidoController : Controller
{
private IPedido _IRepositorio;
public PedidoController()
: this(new PedidoRepositorio())
{ }
public PedidoController(IPedido repositorio)
{
_IRepositorio = repositorio;
}
[HttpGet]
public ActionResult Editar(int pPedidoID)
{
try
{
Pedido _pedido = _IRepositorio.ObterPedidoPorID(pPedidoID);
return View(_pedido);
}
catch
{
return View(new Pedido());
}
}
}
REPORT - request.aspx
public partial class Pedido : System.Web.UI.Page
{
public IPedido _IRepositorio;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
int _PedidoID = 4;
var _pedido = _IRepositorio.ObterPedidoPorID(_PedidoID);
var reportDS = new ReportDataSource();
reportDS.Name = "dsPedido";
reportDS.Value = _pedido;
rptPedido.LocalReport.DataSources.Add(reportDS);
rptPedido.LocalReport.ReportPath = "Areas/Administrativo/Reports/Pedido.rdlc";
}
}
}