How to re-order a Viewbag, inside the View?

Asked

Viewed 113 times

-1

I’m trying to get a Viewbag that received a list in the controller, be reordered after changing a combobox without the page being reloaded.

Passing the List to 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 Page Answers I am using this Viewbag to popular the answers to the Question in 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 was as follows: inserir a descrição da imagem aqui

Now when the user changes the value in the combobox should reorder these responses without reloading the page, I can send a request in ajax and I’m getting the answer with the ordered objects, but I don’t know what I would have to do for that foreach to reload with the ordered answers: request in Ajax:

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

Controller receiving 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 picking up the controller’s json response, but agr do not know what to do to have that foreach reordered...

1 answer

0

You must clean up your ComboBox and place the ordered items after this

Clear Combobox

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

Add new items

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

You will do it in your job AtualizaRespostas

Browser other questions tagged

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