I have a View that has a dropdownlist and a text box. This dropdownlist is returning your items from a model. After filling these two objects, I make a request that will bring some results that I want to show below, however I am loading another model, and it seems that mvc does not allow this. Would you know a solution to this case? I'll put my codes here.
Thank you.
CONTROLLER
public ActionResult Index()
{
ViewBag.Title = "Home page";
TiposEventos eventos = new TiposEventos();
ViewBag.listaEventos = eventos.GetAll().Select(x => new SelectListItem
{
Value = x.idTipoEvento.ToString(),
Text = x.nomeTipoEvento
});
Results res = new Results();
res.listEndossos = null;
res.listSinistros = null;
res.listTotais = null;
return View();
}
[HttpPost]
public ActionResult Index(string idTipoEvento, string dtAnoMes)
{
List<TipoEvento> eventos = new TiposEventos().GetAll().ToList();
string nome = eventos.Find(e => e.idTipoEvento == Convert.ToInt32(idTipoEvento)).nomeTipoEvento;
nome = nome.Replace("\r\n", "");
ViewBag.listaEventos = new SelectList(eventos, "idTipoEvento", "nomeTipoEvento", idTipoEvento);
Results res = new Results();
res.listEndossos = RetornaEndossos(nome, dtAnoMes);
res.listSinistros = RetornaSinistros(nome, dtAnoMes);
res.listTotais = RetornaDadosTotais(nome, dtAnoMes);
return View();
}
VIEW
@{
ViewBag.Title = "Home Page";
}
@section featured {
<section class="featured">
<div class="content-wrapper">
<hgroup class="title">
<h1>@ViewBag.Title</h1>
<h2>@ViewBag.Message</h2>
</hgroup>
@using (Html.BeginForm())
{
<p>
Evento:
@model TipoEvento
@Html.DropDownListFor(m => m.idTipoEvento, (IEnumerable<SelectListItem>)ViewBag.listaEventos, "Selecione um evento")
</p>
<p>
Ano/Mês:
<input type="text" name="dtAnoMes" id="dtAnoMes" placeholder="____/__" maxlength="7" />
</p>
<p>
<button type="submit" class="btn btn-default">Gerar</button>
</p>
}
</div>
</section>
}
@model Results
<h3>Analítico:</h3>
<table class="table table-bordered">
<thead>
<tr>
<th>Num SAP</th>
<th>Valor SAP</th>
<th>Num CRC</th>
<th>Valor CRC</th>
</tr>
</thead>
<tbody>
@foreach (var item in @Model.listEndossos)
{
<tr>
<th scope="row">@item.numEndossoSap</th>
<td>@item.valorSap</td>
<td>@item.numEndossoCRC</td>
<td>@item.valorCRC</td>
</tr>
}
@foreach (var item in @Model.listSinistros)
{
<tr>
<th scope="row">@item.numSinistroSap</th>
<td>@item.valorSap</td>
<td>@item.numSinistroCRC</td>
<td>@item.valorCRC</td>
</tr>
}
</tbody>
</table>
<h3>Total:</h3>
<table class="table table-bordered">
<thead>
<tr>
<th>Ano/Mês</th>
<th>Valor SAP</th>
<th>Valor CRC</th>
<th>Divergência</th>
</tr>
</thead>
<tbody>
@foreach (var item in @Model.listTotais)
{
<tr>
<th scope="row">@item.dtAnoMes</th>
<td>@item.valorSapTotal</td>
<td>@item.valorCRCTotal</td>
<td>@item.divergencia</td>
</tr>
}
</tbody>
</table>
SOLUTION
I was able to resolve using ViewData, but it would also work using ViewBag or TempData. In the view you do not need to pass the model, just the viewdata you created in the controller.
View
@if (ViewData["DadosTotais"] != null)
{
foreach (var item in ViewData["DadosTotais"] as List<ProjetoContabil.Models.Entities.DadosTotais>)
Controller
ViewData["DadosTotais"] = res.listTotais;