List table with ASP.NET MVC condition

2

I am studying ASP.NET MVC5, so I made a Model called 'ReportTagModels' containing:

 public class RelatorioTagModels
{
    [Key]
    public int TagID { get; set; }


    [Required]
    public string Tag { get; set; }
    [Required]
    public string Vedacao { get; set; }
    [Required]
    public string Fluido { get; set; }        
    [Required]
    public string Criticidade { get; set; }
    [Required]       
    public decimal Mtbf { get; set; }
}

View of Model RelatorioTagModels

body>
<p>
    @*@Html.ActionLink("Create New", "Create")*@
</p>
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Tag)
        </th> 
        <th>
            @Html.DisplayNameFor(model => model.Fluido)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Vedacao)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Criticidade)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Mtbf)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <th>
            @Html.DisplayFor(modelItem => item.Tag)
        </th>
        <td>
            @Html.DisplayFor(modelItem => item.Fluido)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Vedacao)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Criticidade)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Mtbf)
        </td>
        <td>              
            @Html.ActionLink("Detalhes", "Index", "RelatorioRa") 
        </td>
    </tr>
}

</table>

By Scaffoding, I generated Controller and Views , where, when I clicked Detalhes , I'm directed to another table, where in this table I would like to list only the records containing% Tag .

Then I created another model called: RelatorioTagModels :

  public class RelatorioRaModels
{
    [Key]
    public int RaID { get; set; }


    [Required]
    public string Data { get; set; }
    [Required]
    public string Nivel { get; set; }
    [Required]
    public string Nº { get; set; }
    [Required]
    public string Tag { get; set; }
}

That by Scaffoding, I created RelatorioRaModels and Controller

My question is, how to list in the model Views only the records of the selected TAG in the model RelatorioRaModels

Update:

View the model: RelatorioTagModels

WhenIclickontheRelatorioTagModelslinkonthefirstline,IwouldliketolistonlytherecordsthatbelongtotheTAG#Detalhe,butatthemoment,itfollowsthewholetable.

Update: Method ActionResult Detail:

[Authorize]
    public async Task<ActionResult> Details(int? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        RelatorioTagModels relatorioTagModels = await db.RelatorioTagModels.FindAsync(id);
        if (relatorioTagModels == null)
        {
            return HttpNotFound();
        }
        return View(relatorioTagModels);
    }

Update:

When I click detail in the first report ( P401-1E ) I'm directed to the RelatorioTagModels of the second report ( Index ), where I want to show only the records that belong to that TAG.

So if I go back in the first report, I click on details of other TAG, I'm directed to the second report where I need to show only the TAG records that were selected in the first report.

It's like I made a RelatorioRaModels in SQL. How do I WHERE when calling the index of the WHERE model

    
asked by anonymous 18.02.2017 / 00:59

2 answers

2

Well, as I said, what you need is basically what @Gigano has already answered here link .

An example I created for your case is as follows.

You will need a ViewModel for your search

public class RelatoriosTagsViewModel
{
    public string Tag { get; set; }

    public IEnumerable<RelatorioTagModels> RelatoriosTags { get; set; }
}

This ViewModel will be used in Index of Controller .

public async Task<ActionResult> Index(RelatoriosTagsViewModel model)
{    
    if (model == null)
    {
        model = new RelatoriosTagsViewModel();
    }

    var query = db.RelatoriosTags.AsQueryable();

    if (!String.IsNullOrWhiteSpace(model.Tag))
    {
        // cria seu select * from Tag = 'P401-1E', mas somente quando a Tag for preenchida na pesquisa
        query = query.Where(a => a.Tag.Equals(model.Tag));
    }

    model.RelatoriosTags = await query.ToListAsync();

    return View(model);
}

With this, you just add Form to Index.cshtml with the filter. To redirect to the RelatoriosRas list I added a ActionLink , which redirects to Index of RelatoriosRasController .

@using RelatoriosTags.ViewModels
@model RelatoriosTagsViewModel

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>

@using (Html.BeginForm("Index", "RelatoriosTags", FormMethod.Get))
{
    <div class="form-group">
        @Html.LabelFor(model => model.Tag, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Tag, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Tag, "", new { @class = "text-danger" })
        </div>
    </div>

    <button type="submit" id="pesquisar" class="btn btn-primary btn-xs">Buscar</button>
}

<table class="table">
    <tr>
        <th>
            Tags
        </th>
        <th>
            Vedação
        </th>
        <th>
            Fluído
        </th>
        <th>
            Criticidade
        </th>
        <th>
            Mtbf
        </th>
        <th></th>
    </tr>

    @foreach (var item in Model.RelatoriosTags)
    {
        <tr>
            <td>
                @Html.ActionLink(linkText: item.Tag, actionName: "Index", controllerName: "RelatoriosRas", routeValues: new { tagId = item.TagID }, htmlAttributes: new { })
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Vedacao)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Fluido)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Criticidade)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Mtbf)
            </td>
            <td>
                @Html.ActionLink("Edit", "Edit", new { id = item.TagID }) |
                @Html.ActionLink("Details", "Details", new { id = item.TagID }) |
                @Html.ActionLink("Delete", "Delete", new { id = item.TagID })
            </td>
        </tr>
    }

</table>

The Index of RelatoriosRasController is as follows

public async Task<ActionResult> Index(int? tagId)
{
    var relatorioRaModels = db.RelatoriosRas.Include(r => r.RelatorioTag);

    if (tagId.HasValue)
    {
        //realiza o filtro para a tag selecionada
        relatorioRaModels = relatorioRaModels.Where(a => a.TagID == tagId);
    }

    return View(await relatorioRaModels.ToListAsync());
}

O Index.cshtml

@model IEnumerable<RelatoriosTags.Models.RelatorioRaModels>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.RelatorioTag.Tag)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Data)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Nivel)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Nº)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.RelatorioTag.Tag)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Data)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Nivel)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Nº)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.RaID }) |
            @Html.ActionLink("Details", "Details", new { id=item.RaID }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.RaID })
        </td>
    </tr>
}

</table>

But I made a change in your modeling, creating a relationship between your two Models.

No RelatorioTagModels I added

public ICollection<RelatorioRaModels> RelatoriosRas { get; set; }

No RelatorioRaModels I've changed

//[Required]
//public string Tag { get; set; }

public int TagID { get; set; }

[ForeignKey("TagID")]
public virtual RelatorioTagModels RelatorioTag { get; set; }

The full example code you can see in the github

    
18.02.2017 / 13:08
1

The error is in the Detail link.

Notice that in your Controller you expect an "id" - that you also allow null to declare it as nullable .

If id is not allowed to be null , which apparently is the case, you only have to wait for a int type and not int? .

The main modification comes here:

@Html.ActionLink("Detalhes", "Index", "RelatorioRa", new { id = item.TagID }) 

In this way you will pass id as parameter GET .

And it would also remove% with% from nullable :

[Authorize]
public async Task<ActionResult> Details(int id)
{
    if (id == null)
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
    var relatorioTagModels = await db.RelatorioTagModels.FindAsync(id);

    if (relatorioTagModels == null)
        return HttpNotFound();

    return View(relatorioTagModels);
}
    
18.02.2017 / 02:09