0
Bring information to the View()
Everybody, good afternoon.
I have the following difficulty: I have a search screen of a Helpdesk and I need to display the database data, which is a list of calls, but in this same screen I have some inputs that will be used to search the database and return this same listing, among them some <selects/>
as, Category, Status and Level priority that in my idea I want to fill with data from the database itself, since each one is a table. I was able to bring the data to the inputs through a Viewmodel, but I can not list the calls from Viewmodel, nor create a Viewbag with the list returned from the database to do this listing and apparently not bring more than one data as parameter by View().
Then my doubt would be:
- How to pass more than one parameter to view or
- How to list my calls and at the same time fill the selects with viewModel itself
I don’t know if I’m being clear enough, I apologize from now on, you’re confused for me too.
Follow the code to illustrate:
//Code referring to my View
@model AppHelpDesk.Models.ViewModels.ChamadoViewModel
@{
}
<div class="d-flex flex-row mb-3">
<div class="input-group input-group-md col-6">
<div class="input-group-prepend">
<label asp-for="Chamado.Id" class="input-group-text" id="inputGroup-sizing-sm"></label>
</div>
<input asp-for="Chamado.Id" value="" placeholder="Id do chamado" type="text" class="form-control" aria-label="Sizing example input" aria-describedby="inputGroup-sizing-sm">
</div>
<div class="input-group input-group-md col-6">
<div class="input-group-prepend">
<label asp-for="Chamado.Titulo" class="input-group-text" id="inputGroup-sizing-sm">Titulo</label>
</div>
<input asp-for="Chamado.Titulo" value="" placeholder="Titulo do chamado" type="text" class="form-control" aria-label="Sizing example input" aria-describedby="inputGroup-sizing-sm">
</div>
</div>
<div class="d-flex flex-row mb-3">
<div class="input-group input-group-md col-4">
<div class="input-group-prepend">
<label asp-for="Chamado.DataInicial" class="input-group-text" id="inputGroup-sizing-sm">Data Inicial</label>
</div>
<input asp-for="Chamado.DataInicial" class="form-control" type="date" value="" id="example-date-input">
</div>
<div class="input-group input-group-md col-4">
<div class="input-group-prepend">
<label asp-for="Chamado.DataFinal" class="input-group-text" id="inputGroup-sizing-sm">Data Final</label>
</div>
<input asp-for="Chamado.DataFinal" class="form-control" type="date" value="" id="example-date-input">
</div>
<div class="input-group input-group-md col-4">
<div class="input-group-prepend">
<label asp-for="Chamado.CategoriaId" class="input-group-text"
for="inputGroupSelect01">Categoria</label>
</div>
<select asp-for="Chamado.CategoriaId"
asp-items="@(new SelectList(Model.Categoria, "Id", "Nome"))"
class="custom-select" id="inputGroupSelect01">
</select>
</div>
</div>
<div class="d-flex flex-row mb-3">
<div class="input-group input-group-md col-5">
<div class="input-group-prepend">
<label asp-for="Chamado.StatusId" class="input-group-text" for="inputGroupSelect01">Status</label>
</div>
<select asp-for="Chamado.StatusId"
asp-items="@(new SelectList(Model.Status, "Id", "Nome"))"
class="custom-select" id="inputGroupSelect01">
</select>
</div>
<div class="input-group input-group-md col-5">
<div class="input-group-prepend">
<label asp-for="Chamado.NivelPrioridadeId" class="input-group-text" for="inputGroupSelect01">Prioridade</label>
</div>
<select asp-for="Chamado.NivelPrioridadeId"
asp-items="@(new SelectList(Model.NivelPrioridade, "Id", "Nome"))"
class="custom-select" id="inputGroupSelect01">
</select>
</div>
<div class="input-group input-group-md col-2 d-flex justify-content-end">
<button type="submit" class="btn btn-primary">Pesquisar</button>
</div>
</div>
<div class="bg-white p-4 table-responsive-md">
<h4 class="mb-3">
Chamados pendentes
</h4>
@*<table class="table table-bordered table-sm table-hover">
<thead class="thead-dark">
<tr>
<th scope="col">
@Html.DisplayNameFor(model => model.Titulo)
</th>
<th scope="col">
@Html.DisplayNameFor(model => model.Descricao)
</th>
<th scope="col">
@Html.DisplayNameFor(model => model.DataInicial)
</th>
<th scope="col">
@Html.DisplayNameFor(model => model.DataFinal)
</th>
</tr>
</thead>
<tbody>
@foreach (var i in Model)
{
if (i.StatusId == 1)
{
<tr>
<td>@Html.DisplayFor(modelItem => i.Titulo)</td>
<td>@Html.DisplayFor(modelItem => i.Descricao)</td>
<td>@Html.DisplayFor(modelItem => i.DataInicial)</td>
<td>@Html.DisplayFor(modelItem => i.DataFinal)</td>
</tr>
}
}
</tbody>
</table>*@
</div>
...
// Code from my controller
namespace AppHelpDesk.Controllers
{
public class HomeController : Controller
{
private readonly DashboardService _dashboardService;
public HomeController (DashboardService dashboardService)
{
this._dashboardService = dashboardService;
}
// GET: Dashboard
public IActionResult Index()
{
var list = _dashboardService.FindAll();
ViewBag.pedingCount = list.Where(x => x.StatusId == 1).Count();
ViewBag.SolvedCount = list.Where(x => x.StatusId == 2).Count();
ViewBag.priorityCount = list.Where(x => x.NivelPrioridadeId == 3).Count();
return View();
}
public IActionResult Create(Chamado chamado)
{
_dashboardService.Insert(chamado);
return RedirectToAction(nameof(Index));
}
public IActionResult Pending(Chamado chamado)
{
var status = _dashboardService.FindAllStatus();
var prioridade = _dashboardService.FindAllPrioridade();
var categoria = _dashboardService.FindAllCategoria();
var setor = _dashboardService.FindAllSetor();
var viewModel = new ChamadoViewModel
{
Categoria = categoria,
Chamado = chamado,
NivelPrioridade = prioridade,
Setor = setor,
Status = status
};
ViewBag.listaChamados = _dashboardService.FindAll();
return View(viewModel);
}
public IActionResult Solved()
{
var list = _dashboardService.FindAll();
return View(list);
}
public IActionResult Priority()
{
var list = _dashboardService.FindAll();
return View(list);
}
}
}
// Code of my Viewmodel
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace AppHelpDesk.Models.ViewModels
{
public class ChamadoViewModel
{
public Chamado Chamado { get; set; }
public ICollection<Categoria> Categoria { get; set; }
public ICollection<Status> Status { get; set; }
public ICollection<Setor> Setor { get; set; }
public ICollection<NivelPrioridade> NivelPrioridade { get; set; }
}
}