-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>
}
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...
Replace images with code
– Leandro Angelo