我正在尝试在视图中获取模型并将其传递给另一个控制器,但模型在传递给另一个控制器时为null。
控制器 - 在这里,我将模型发送到我的视图中呈现:
[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;
}
}
答案 0 :(得分:0)
您无法在请求(操作)之间传递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个参数,我将以某种结构组织这些对象。