Ordination down the line!

Asked

Viewed 85 times

2

On my cshtml page I have a foreach that reads this information from the database and plots on the screen in the form of checkbox, I’m trying to order or revert in decreasing form on the screen, but I’m not getting, follow the information below the table data:

1 Quantity of Water

2 Quantity of Foam

3 Fire extinguisher

4 Sand/Earth

5 Public

6 Particular

7 Firefight 8 Use of aircraft

9 Damper

11 Removal of Material

I wanted to do in my Razor read 11 to 1 to appear on the screen, someone could give me a help, follow the code:

@using SDO.Domain.Entities
@model List<OcorrenciaIncendioModoExtincao>
@{
    var modo1 = Model.FirstOrDefault(m => m.ModoExtincaoId == 1);
    var modo2 = Model.FirstOrDefault(m => m.ModoExtincaoId == 2);
    var modo5 = Model.FirstOrDefault(m => m.ModoExtincaoId == 5);
    var modo6 = Model.FirstOrDefault(m => m.ModoExtincaoId == 6);
}
<div class="panel panel-default">
    <div class="panel-heading">
        <h4 class="panel-title">Modo Extinção</h4>
    </div>
    <div class="input-container panel-body" name="FormModoExtincao">
        <div class="col-sm-12">
            @foreach (var modo in ViewBag.ModoExtincao as List<ModoExtincao> )
            {
                if (modo.Id == 5)
                {
                    <div class="clearfix"></div>
                    <div class="col-sm-3">
                        
                    </div>
                }
                <div class="col-sm-3" name="[email protected]">
                    <p>
                        <label>
                            <input type="checkbox"
                                   classname="ModosExtincao"
                                   name="ModoExtincaoId"
                                   id="@modo.Id"
                                   value="@modo.Id"
                                   @(modo.Dependencia.HasValue ? "data-dep=" + modo.Dependencia.Value + " ignore" : "data-dep=0")
                                   @(Model.FirstOrDefault(a => a.ModoExtincaoId == modo.Id) != null ? "checked" : "")
                                   @(modo.Dependencia.HasValue && Model.FirstOrDefault(a => a.ModoExtincaoId == modo.Dependencia) != null && ((modo.Id == 5 || modo.Id == 2) && Model.FirstOrDefault(a => a.ModoExtincaoId == modo.Dependencia).QtdConsumidaPublica > 0) ? "checked" : "" )
                                   @(modo.Dependencia.HasValue && Model.FirstOrDefault(a => a.ModoExtincaoId == modo.Dependencia) != null && (modo.Id == 6 && Model.FirstOrDefault(a => a.ModoExtincaoId == modo.Dependencia).QtdConsumidaParticular > 0) ? "checked" : "" ) />

                            @modo.Descricao
                        </label>
                        @if (modo.Id == 2 || modo.Id == 5 || modo.Id == 6) //ESPUMA, PUBLICA, PARTICULAR
                        {
                        <div class="complemento" data-dep="@modo.Id">
                            <span>@(modo.Id == 2 ? "Quantidade consumida (Litros)" : "Quantidade consumida (m³)")</span>
                            <div>
                                <input type="number"
                                       classname="ModosExtincao"
                                       class="form-control form-control-80"
                                       name="[email protected]"
                                       data-dep="@modo.Id"
                                       belongs-to="@(modo.Dependencia.HasValue ? modo.Dependencia : modo.Id)"
                                       searchfor="ModoExtincaoId"
                                       max="99999"
                                       maxlength="5"
                                       value="@((modo2 != null && modo.Id == 2 && modo2.QtdConsumidaEspuma != null)
	                                                    ? modo2.QtdConsumidaEspuma.ToString()
                                                            : (modo1 != null && modo.Id == 5 && modo1.QtdConsumidaPublica != null )
                                                            ? modo1.QtdConsumidaPublica.ToString()
                                                                : (modo1 != null && modo.Id == 6 && modo1.QtdConsumidaParticular != null)
                                                                    ? modo1.QtdConsumidaParticular.ToString()
                                                                    : "")" />
                            </div>
                            @if (modo.Id == 6)
                            {
                                var ed = Model.FirstOrDefault(m => m.ModoExtincaoId == modo.Dependencia);
                                <div>
                                    <span>Do próprio imóvel?</span>
                                    <label><input type="radio" name="ProprioImovel" value="1" @(ed != null && string.IsNullOrEmpty(ed.EnderecoOutraEdificacao) ? "checked" : "" ) />Sim</label>
                                    <label><input type="radio" name="ProprioImovel" value="0" @(ed != null && !string.IsNullOrEmpty(ed.EnderecoOutraEdificacao) ? "checked" : "" ) />Não</label>
                                    <div class="complemento">
                                        <span>Endereço da edificação da qual usou a água: </span>
                                        <input type="text"
                                               classname="ModosExtincao"
                                               class="form-control"
                                               belongs-to="@modo.Dependencia"
                                               searchfor="ModoExtincaoId"
                                               maxlength="80"
                                               name="EnderecoOutraEdificacao"
                                               value="@(ed != null ? ed.EnderecoOutraEdificacao : "" )" />
                                    </div>
                                </div>
                            }
                        </div>
                        }
                    </p>&nbsp;
                </div>
            }
        </div>
    </div>
</div>

Thank you.

  • You want to sort down the field ModoExtincaoId?

  • It would be in the foreach mode variable, all ids passes one by one mode

  • But this variable is an object list of the type ModoExtincao, would you like to sort descending from which field of that class? For example, by identifier, by name, by description. Tell me the name of the field please

  • Ah ok, so I’ll never be able to sort, because the mode variable would be an object list!

  • Yes you can, but I want to know by which field you would like to order rsrs. Tell me the name of the attribute you want to order.

  • In the Text class I have the fields Id, Description and Dependency, can be sorted by id

  • See if this helps you. It changes the order of the html elements, the last is first and so on. https://answall.com/questions/350191/ordenar-cadastros-adicionados-recentemente-dinamicamente/350203#350203

Show 2 more comments

1 answer

1


As discussed in the comments, you would like to sort decreasingly by the identifier of your objects. To do this, change the line of your foreach as example below, so we will perform the ordering via Linq:

@foreach (var modo in (ViewBag.ModoExtincao as List<ModoExtincao>).OrderByDescending(o => o.Id))
{
    //Aqui continua o fluxo já existente...
}

If you want to add more fields in the sort, use the method ThenByDescending for descending order and ThenBy for ascending order. You can use as many times as needed:

@foreach (var modo in (ViewBag.ModoExtincao as List<ModoExtincao>).OrderByDescending(o => o.Id).ThenByDescending(o => o.Descricao).ThenByDescending(o.Dependencia))
{
    //Aqui continua o fluxo já existente...
}
  • Thank you Peter Paul for the strength and all who helped me.

  • Tamo along bro!

Browser other questions tagged

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