List table with condition ASP.NET MVC

Asked

Viewed 1,081 times

2

I’m studying ASP.NET MVC5, so I did one Model called 'Reporteriotagmodels' 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 do 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 ran the Controller and the Views, where, by clicking on Detalhes, I am directed to another table, where in this table, I would like to list only the records that contain the Tag model’s RelatorioTagModels.

So I created another model called: RelatorioRaModels:

  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 the Controller and the Views

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

Updating:

Model view: RelatorioTagModels

Primeiro Relatório

By clicking on the link Detalhe of the first line, I would like to list only the records that belong to the TAG P401-1E, but, at the moment, it brings the whole table.

Segundo Relatório

Update: Actionresult of Detailing Method:

[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);
    }

Updating:

By clicking detail in the first report (RelatorioTagModels) I am directed to the Index of the second report (RelatorioRaModels), where I want to show only the records that belong to that TAG.

If I go back in the first report, click on details of another TAG, I am directed to the second report, where I need it to show only the TAG records that were selected in the first report.

It’s like I’m making a WHERE in SQL. How do I do this WHERE when calling the index of the model RelatorioRaModels

  • Like this? http://answall.com/questions/102939/guardar-e-enviar-id-de-item-selected-no-dropdowlinst/102945#102945

  • It’s not really a survey, I’ll improve the question, thank you

  • Edit your question and put the ActionResult of the method of Detailing.

  • Friend, I updated the question as requested

  • I don’t understand why you save the report in bank.

  • @The first report generated by the Reporteriotagmodels model is dynamic, the MTBF field is calculated from another application of mine! The second report, generated by the Reporterioramodels model has all the dates and number of each update, and in this second report (Reporterioramodels) I want to "filter", to bring only the TAG records that was clicked on the first report (Reporteriotagmodels)-

  • 1

    You want to return all records of the same tag, as if it were a select * from RelatorioTagModels where Tag = 'P401-1E'?

  • @Pablovargas exact friend, that’s right, only I want to use the table RelatorioRaModels

  • 2

    So basically the link that @Ciganomorrisonmendez put in the first comment. But I will prepare an example and go up on github with your case

  • Man, if you could do that, it would help me a lot. but I’ll read again the link from Gypsy thank you

  • Just to understand, a Reporteriotagmodels has several Reporterioramodels?

Show 7 more comments

2 answers

2


Well, as I said, what you need is basically what @Gypsy has answered here /a/102945/5846.

An example I created for your case is the following.

You will need a ViewModel for your research

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

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

That one 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, just add one Form in Index.cshtml with the filter. To redirect to the list of RelatoriosRas I added a ActionLink, which redirects to the 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>

Already 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());
}

The 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.

In the RelatorioTagModels I added

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

In the RelatorioRaModels i altered

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

public int TagID { get; set; }

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

The complete code of the example you can see in github

1

The error is in the Detail link.

Notice that in your Controller you expect an "id" - which you also allow to be null by declaring him as nullable.

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

The main modification comes here:

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

That way you’ll pass the id as a parameter GET.

And also remove the nullable of Controller:

[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);
}
  • Buddy, that’s not quite it, see, I want to get the TAG of the first report generated by the model RelatorioTagModels and that by clicking on details, it directs me to the ìndex do model ´RelatorioRaModels, showing only the TAG records of the first report.

Browser other questions tagged

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