View data on a C# ASP.NET search screen

Asked

Viewed 56 times

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:

  1. How to pass more than one parameter to view or
  2. 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; }
    }
}

1 answer

0


How to pass more than one parameter to View

A: It is not possible to pass more than 1 parameter to the View method is precisely why we use Viewmodels for This.

How to list my calls and at the same time fill the Selects with Viewmodel itself

A: Basically your Viewmodel will possess a little more Responsibility that it possesses today. From what I understand you want to make a filter and list the filtered record, basically this will look like this.

Controller

public IActionResult Pending(ChamadoViewModel viewModel)
{
    return View(viewModel.Get());
}

View Model

public class ChamadoViewModel
{
    #region Dependency Injection

    private readonly IRepository _repository;

    public ChamadoViewModel(IRepository repository)
    {
        _repository = repository;
    }

    #endregion

    #region Handlers

    public ChamadoViewModel Get()
    {
        var query = _repository.FindAllChamados()
                                .AsQueryable();

        Categorias = _repository.FindAllCategorias();
        Status = _repository.FindAllStatus();
        Setores = _repository.FindAllSetores();
        NivelPrioridades = _repository.FindAllPrioridades();

        if (Id != null)
            query = query.Where(x => x.Id == Id);
        if (string.IsNullOrEmpty(Titulo))
            query = query.Where(x => x.Titulo == Titulo);
        if (DataInicial != null && DataFinal != null)
            query = query.Where(x => x.DataInicial >= DataInicial && x.DataFinal <= DataFinal);
        if (CategoriaId != null)
            query = query.Where(x => x.CategoriaId == CategoriaId);
        if (StatusId != null)
            query = query.Where(x => x.StatusId == StatusId);
        if (SetorId != null)
            query = query.Where(x => x.SetorId == SetorId);
        if (NivelPrioridadeId != null)
            query = query.Where(x => x.NivelPrioridadeId == NivelPrioridadeId);

        Chamados = query.ToList();
        return this;
    }

    #endregion

    #region Data

    public Chamado Chamado { get; set; }
    public ICollection<Chamado> Chamados { get; set; }
    public ICollection<Categoria> Categorias { get; set; }
    public ICollection<Status> Status { get; set; }
    public ICollection<Setor> Setores { get; set; }
    public ICollection<NivelPrioridade> NivelPrioridades { get; set; }

    #endregion

    #region Helper

    // Essa é a região de Filtro é nessas propriedades que serão feito os Binds ou Asp-For

    public int? Id { get; set; }
    public int? CategoriaId { get; set; }
    public int? StatusId { get; set; }
    public int? SetorId { get; set; }
    public int? NivelPrioridadeId { get; set; }
    public string Titulo { get; set; }
    public DateTime? DataInicial { get; set; }
    public DateTime? DataFinal { get; set; }

    #endregion
}

View

@model AspNetCore.Controllers.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="Id" class="input-group-text" id="inputGroup-sizing-sm"></label>
        </div>
        <input asp-for="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="Titulo" class="input-group-text" id="inputGroup-sizing-sm">Titulo</label>
        </div>
        <input asp-for="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="DataInicial" class="input-group-text" id="inputGroup-sizing-sm">Data Inicial</label>
        </div>
        <input asp-for="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="DataFinal" class="input-group-text" id="inputGroup-sizing-sm">Data Final</label>
        </div>
        <input asp-for="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="CategoriaId" class="input-group-text"
                   for="inputGroupSelect01">Categoria</label>
        </div>
        <select asp-for="CategoriaId"
                asp-items="@(new SelectList(Model.Categorias, "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="StatusId" class="input-group-text" for="inputGroupSelect01">Status</label>
        </div>
        <select asp-for="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="NivelPrioridadeId" class="input-group-text" for="inputGroupSelect01">Prioridade</label>
        </div>
        <select asp-for="NivelPrioridadeId"
                asp-items="@(new SelectList(Model.NivelPrioridades, "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.Chamado.Titulo)
                    </th>
                    <th scope="col">
                        @Html.DisplayNameFor(model => model.Chamado.Descricao)
                    </th>
                    <th scope="col">
                        @Html.DisplayNameFor(model => model.Chamado.DataInicial)
                    </th>
                    <th scope="col">
                        @Html.DisplayNameFor(model => model.Chamado.DataFinal)
                    </th>
                </tr>
            </thead>
            <tbody>

                @foreach (var i in Model.Chamados)
                {
                    if (Model.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>

I hope that clarifies all your doubts, and if any remain open I am willing to try to clarify it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.