我如何 return 带有 ActionLink 的模型?
How can I return a model with an ActionLink?
我试图在视图中获取一个模型并将其传递给另一个控制器,但是传递给另一个控制器时模型为空。
控制器 - 在这里我发送模型在我的视图中渲染:
[HttpPost]
public PartialViewResult Index(ReportesTabularesViewModel ModeloInput)
{
GetDatosTabularReportInput input = new GetDatosTabularReportInput { IdSensor = ModeloInput.sensor, FechaInicio = ModeloInput.FechaInicio, FechaFinal = ModeloInput.FechaFinal };
ReportesTabularesViewModel Modelo = new ReportesTabularesViewModel();
var Lista = new CaelusReporting.Datos.DatosApp().GetDatosTabularReport(input);
var s = Modelo.Sensores;
ViewBag.Sensores = s;
Modelo.Datos = Lista.GroupBy(x => x.Fecha).Select(y => new DatosViewModel
{
Fecha = y.Key,
EstacionSensorSensorNombre = y.First().EstacionSensorSensorNombre,
Datos = y
}
);
ViewBag.Modelo = ModeloInput;
return PartialView(Modelo);
}
查看:
@model CaelusReporting.Web.Models.ViewModels.Datos.ReportesTabularesViewModel
@{
ViewData["temp"] = ViewBag.Modelo;
ViewBag.Title = Model.Datos.Select(y => y.EstacionSensorSensorNombre).FirstOrDefault();
List<CaelusReporting.Sensores.Dto.SensoresDto> sensores = ViewBag.Sensores;
}
<a class="menu-bar" data-toggle="collapse" href="#menu">
<span class="bars"></span>
</a>
<div class="collapse menu" id="menu">
<div class="list-inline">
@using (Ajax.BeginForm("Index", new AjaxOptions { UpdateTargetId = "Update", HttpMethod = "POST", InsertionMode = InsertionMode.Replace }))
{
<label>Sensor: </label>
<select data-placeholder="Escoja las estaciones" class="chosen-select-width" tabindex="8" name="sensor">
@foreach (var item in sensores.Where(x => x.estado == true))
{
<option value=@item.Id>@item.nombre</option>
}
</select>
<label>Fecha de Inicio: </label>
@Html.TextBoxFor(m => m.FechaInicio, null, new { type = "datetime-local", style = "max-width:235px; max-height:20px" })
<label>Fecha de Final: </label>
@Html.TextBoxFor(m => m.FechaFinal, null, new { type = "datetime-local", style = "max-width:235px; max-height:20px" })
<input id="Submit1" type="submit" value="Ver" />
}
<a href="@Url.Action("ExportReport", "Reportes", new { DocType = "PDF" })"> Get Report in PDF</a>
<a href="@Url.Action("ExportReport", "Reportes", new { DocType = "XLS" })"> Get Report in XLS</a>
<a href="@Url.Action("ExportReport", "Reportes", new { DocType = "DOC" })"> Get Report in DOC</a>
<a href="@Url.Action("ExportReport", "Reportes", new { DocType = "CSV" })"> Get Report in CSV</a>
</div>
</div>
另一个控制器 - 我在这里尝试导入位于以下位置的报告:
public ActionResult ExportReport(string DocType)
{
ReportesTabularesViewModel test = ViewData["temp"] as ReportesTabularesViewModel;
GetDatosTabularReportInput input = new GetDatosTabularReportInput { IdSensor = test.sensor, FechaInicio = test.FechaInicio, FechaFinal = test.FechaFinal };
var Lista = new CaelusReporting.Datos.DatosApp().GetDatosTabularReport(input);
ReportDocument rd = new ReportDocument();
rd.Load(Path.Combine(Server.MapPath("~/Reportess"), "Reporte.rpt"));
rd.SetDataSource(Lista);
Response.Buffer = false;
Response.ClearContent();
Response.ClearHeaders();
switch (DocType)
{
case "PDF":
rd.Load(Path.Combine(Server.MapPath("~/Reportess"), "Reporte.rpt"));
try
{
Stream stream = rd.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);
stream.Seek(0, SeekOrigin.Begin);
return File(stream, "application/pdf", "ReportePDF.pdf");
}
catch (Exception ex)
{
throw;
}
case "DOC":
rd.Load(Path.Combine(Server.MapPath("~/Reportess"), "Reporte.rpt"));
try
{
Stream stream = rd.ExportToStream(CrystalDecisions.Shared.ExportFormatType.WordForWindows);
stream.Seek(0, SeekOrigin.Begin);
return File(stream, "application/msword", "ReporteDOC.doc");
}
catch (Exception ex)
{
throw;
}
case "XLS":
rd.Load(Path.Combine(Server.MapPath("~/Reportess"), "Reporte.rpt"));
try
{
Stream stream = rd.ExportToStream(CrystalDecisions.Shared.ExportFormatType.Excel);
stream.Seek(0, SeekOrigin.Begin);
return File(stream, "application/vnd.ms-excel", "ReporteDOC.xls");
}
catch (Exception ex)
{
throw;
}
case "CSV" :
rd.Load(Path.Combine(Server.MapPath("~/Reportess"), ""));
try
{
Stream stream = rd.ExportToStream(CrystalDecisions.Shared.ExportFormatType.CharacterSeparatedValues);
stream.Seek(0, SeekOrigin.Begin);
return File(stream, "text/csv", "ReporteCSV.csv");
}
catch (Exception ex)
{
throw;
}
default:
return null;
}
}
您不能在请求(操作)之间传递 ViewData。您需要以某种方式序列化数据,例如在查询中。您可以使用 RouteValueDictionary
来执行此操作。
您需要像这样为操作 ActionResult ExportReport(string DocType)
创建模型:
public class ExportReportModel
{
public string DocType {get; set;}
// all fields which you required from ReportesTabularesViewModel
}
那么您的操作将如下所示 ActionResult ExportReport(ExportReportModel model)
并且您可以呈现这样的链接:
<a href="@Url.Action("ExportReport", "Reportes", new RouteValueDictionary(new ReportesTabularesViewModel{/*initialize object*/}))">Get Report in PDF</a>
你也可以使用匿名对象,但如果你有超过 3 个参数,我会以某种结构组织它们。
我试图在视图中获取一个模型并将其传递给另一个控制器,但是传递给另一个控制器时模型为空。
控制器 - 在这里我发送模型在我的视图中渲染:
[HttpPost]
public PartialViewResult Index(ReportesTabularesViewModel ModeloInput)
{
GetDatosTabularReportInput input = new GetDatosTabularReportInput { IdSensor = ModeloInput.sensor, FechaInicio = ModeloInput.FechaInicio, FechaFinal = ModeloInput.FechaFinal };
ReportesTabularesViewModel Modelo = new ReportesTabularesViewModel();
var Lista = new CaelusReporting.Datos.DatosApp().GetDatosTabularReport(input);
var s = Modelo.Sensores;
ViewBag.Sensores = s;
Modelo.Datos = Lista.GroupBy(x => x.Fecha).Select(y => new DatosViewModel
{
Fecha = y.Key,
EstacionSensorSensorNombre = y.First().EstacionSensorSensorNombre,
Datos = y
}
);
ViewBag.Modelo = ModeloInput;
return PartialView(Modelo);
}
查看:
@model CaelusReporting.Web.Models.ViewModels.Datos.ReportesTabularesViewModel
@{
ViewData["temp"] = ViewBag.Modelo;
ViewBag.Title = Model.Datos.Select(y => y.EstacionSensorSensorNombre).FirstOrDefault();
List<CaelusReporting.Sensores.Dto.SensoresDto> sensores = ViewBag.Sensores;
}
<a class="menu-bar" data-toggle="collapse" href="#menu">
<span class="bars"></span>
</a>
<div class="collapse menu" id="menu">
<div class="list-inline">
@using (Ajax.BeginForm("Index", new AjaxOptions { UpdateTargetId = "Update", HttpMethod = "POST", InsertionMode = InsertionMode.Replace }))
{
<label>Sensor: </label>
<select data-placeholder="Escoja las estaciones" class="chosen-select-width" tabindex="8" name="sensor">
@foreach (var item in sensores.Where(x => x.estado == true))
{
<option value=@item.Id>@item.nombre</option>
}
</select>
<label>Fecha de Inicio: </label>
@Html.TextBoxFor(m => m.FechaInicio, null, new { type = "datetime-local", style = "max-width:235px; max-height:20px" })
<label>Fecha de Final: </label>
@Html.TextBoxFor(m => m.FechaFinal, null, new { type = "datetime-local", style = "max-width:235px; max-height:20px" })
<input id="Submit1" type="submit" value="Ver" />
}
<a href="@Url.Action("ExportReport", "Reportes", new { DocType = "PDF" })"> Get Report in PDF</a>
<a href="@Url.Action("ExportReport", "Reportes", new { DocType = "XLS" })"> Get Report in XLS</a>
<a href="@Url.Action("ExportReport", "Reportes", new { DocType = "DOC" })"> Get Report in DOC</a>
<a href="@Url.Action("ExportReport", "Reportes", new { DocType = "CSV" })"> Get Report in CSV</a>
</div>
</div>
另一个控制器 - 我在这里尝试导入位于以下位置的报告:
public ActionResult ExportReport(string DocType)
{
ReportesTabularesViewModel test = ViewData["temp"] as ReportesTabularesViewModel;
GetDatosTabularReportInput input = new GetDatosTabularReportInput { IdSensor = test.sensor, FechaInicio = test.FechaInicio, FechaFinal = test.FechaFinal };
var Lista = new CaelusReporting.Datos.DatosApp().GetDatosTabularReport(input);
ReportDocument rd = new ReportDocument();
rd.Load(Path.Combine(Server.MapPath("~/Reportess"), "Reporte.rpt"));
rd.SetDataSource(Lista);
Response.Buffer = false;
Response.ClearContent();
Response.ClearHeaders();
switch (DocType)
{
case "PDF":
rd.Load(Path.Combine(Server.MapPath("~/Reportess"), "Reporte.rpt"));
try
{
Stream stream = rd.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);
stream.Seek(0, SeekOrigin.Begin);
return File(stream, "application/pdf", "ReportePDF.pdf");
}
catch (Exception ex)
{
throw;
}
case "DOC":
rd.Load(Path.Combine(Server.MapPath("~/Reportess"), "Reporte.rpt"));
try
{
Stream stream = rd.ExportToStream(CrystalDecisions.Shared.ExportFormatType.WordForWindows);
stream.Seek(0, SeekOrigin.Begin);
return File(stream, "application/msword", "ReporteDOC.doc");
}
catch (Exception ex)
{
throw;
}
case "XLS":
rd.Load(Path.Combine(Server.MapPath("~/Reportess"), "Reporte.rpt"));
try
{
Stream stream = rd.ExportToStream(CrystalDecisions.Shared.ExportFormatType.Excel);
stream.Seek(0, SeekOrigin.Begin);
return File(stream, "application/vnd.ms-excel", "ReporteDOC.xls");
}
catch (Exception ex)
{
throw;
}
case "CSV" :
rd.Load(Path.Combine(Server.MapPath("~/Reportess"), ""));
try
{
Stream stream = rd.ExportToStream(CrystalDecisions.Shared.ExportFormatType.CharacterSeparatedValues);
stream.Seek(0, SeekOrigin.Begin);
return File(stream, "text/csv", "ReporteCSV.csv");
}
catch (Exception ex)
{
throw;
}
default:
return null;
}
}
您不能在请求(操作)之间传递 ViewData。您需要以某种方式序列化数据,例如在查询中。您可以使用 RouteValueDictionary
来执行此操作。
您需要像这样为操作 ActionResult ExportReport(string DocType)
创建模型:
public class ExportReportModel
{
public string DocType {get; set;}
// all fields which you required from ReportesTabularesViewModel
}
那么您的操作将如下所示 ActionResult ExportReport(ExportReportModel model)
并且您可以呈现这样的链接:
<a href="@Url.Action("ExportReport", "Reportes", new RouteValueDictionary(new ReportesTabularesViewModel{/*initialize object*/}))">Get Report in PDF</a>
你也可以使用匿名对象,但如果你有超过 3 个参数,我会以某种结构组织它们。