How to re-order a ViewBag, within View?

-1

I'm trying to make a ViewBag that received a list on the controller, be reordered after changing a combobox without the page being reloaded.

Passing the List to the ViewBag:

public ActionResult Answers(int id)
        {
            List<Respostas> Respostas = _Control.ViewRespostas(id);
            Respostas = Respostas.OrderBy(p => p.DataResposta).ToList();
            Pergunta aPergunta = _Control.SelecionarPergunta(id);
            Usuario oUsuario = _Control.SelecionarUsuario(aPergunta.Per_IDUsuario);
            ViewBag.Usuario = oUsuario.Usu_UserName;
            ViewBag.Quantidade = Respostas.Count();
            ViewBag.Respostas = Respostas;
            ViewBag.LDPergunta = _Control.ViewLikesDeslikesPergunta(id);
            return View(aPergunta);
        }

And on the Answers Page I'm using this ViewBag to populate the answers to the Question Question:

foreach (var x in ViewBag.Respostas)
                    {
                        <hr />
                        <div class="row text-center">
                            <div class="col-md-3">
                                <div align="center">
                                    <img src="@Url.Action("GetImagem", new { codigo = x.Res_IDUsuario })" class="img-circle img-responsive ImagemUsuario" />
                                </div>
                                <h5 class="text-center">@x.Usu_Name</h5>
                                <h5 class="text-center">@x.DataResposta</h5>
                                <span class="btn btn-success Like" onclick="LikeDeslike(@x.Res_ID,'Like')"><span id="quantidadeLikes@(x.Res_ID)">@x.QuantidadeLikes</span> <span class="glyphicon glyphicon-thumbs-up"></span></span>
                                <span class="btn btn-danger Deslike" onclick="LikeDeslike(@x.Res_ID,'Deslike')"><span id="quantidadeDeslikes@(x.Res_ID)">@x.QuantidadeDeslikes</span> <span class="glyphicon glyphicon-thumbs-down"></span></span>
                                <br />
                            </div>

                            <div class="col-md-9">
                                <span align="left">@Html.Raw(@x.Res_Message)</span>
                            </div>
                        </div>
                    }

The result is as follows:

Nowwhentheuserchangesthevalueinthecomboboxshouldreordertheseresponseswithoutreloadingthepage,IcansendarequestinajaxandI'mgettingtheanswerwiththeorderedobjects,butIdonotknowwhattodosothatthisforeachreloadswiththeorderedresponses:requestinAjax:

$("#Filtro").change(function () {
                var Url = "@Url.Action("OrdenaRespostas", "Questions")";
                var Valor = $(this).val();
                $.post(Url, { id: @Model.Per_ID, Filtro: Valor }, AtualizaRespostas);
            });

Controller that receives the request:

   public ActionResult OrdenaRespostas(int id,string Filtro)
        {
            if(Filtro!="")
            {
                List<Respostas> Respostas = _Control.ViewRespostas(id);
                if(Filtro=="recentes")
                {
                    Respostas = Respostas.OrderBy(p => p.DataResposta).ToList();
                }

                else if(Filtro=="antigas")
                {
                    Respostas = Respostas.OrderByDescending(p => p.DataResposta).ToList();
                }

                else if(Filtro=="like")
                {
                    Respostas = Respostas.OrderBy(p => p.QuantidadeLikes).ToList();
                }

                else if(Filtro=="deslike")
                {
                    Respostas = Respostas.OrderBy(p => p.QuantidadeDeslikes).ToList();
                }
                return Json(Respostas);
            }
            return View("Index");
        }

Function that is receiving the response:

function AtualizaRespostas(resposta) {
                $("@ViewBag.Respostas").html(resposta);
            }

This response parameter is getting the answer in json from the controller, but agr I do not know what to do to make that foreach, be reordered ...

    
asked by anonymous 20.12.2018 / 20:10

1 answer

0

You should clean up your ComboBox and put the ordered items after it

Clear ComboBox

$("#IdDaSuaComboBox").empty();

Add new items

$.each(resposta, function (i, obj) {
    $("#IdDaSuaComboBox").append('<option value="' + obj.Value + '">' + obj.Text + '</option>');
});

You will do this in your function AtualizaRespostas

    
27.12.2018 / 17:55