How to return to previous View without losing data

Asked

Viewed 142 times

0

I have a problem that I can’t solve, which is this. I have a screen that loads two simulated (each simulated has the questions and the radios Buttons that are the options). The simulated ones are loaded in a partial view. On the parent screen I have 2 buttons. When I click on 1 button in the partial view, it loads a simulated one and when I click on the other button it presses another simulated one in the partial view. So far so good. The problem is that when I am in a simulated and dopois back to the previous one the options I had marked come back unchecked. How do I get the answers I marked to stay marked? Model:

public class PerguntasOpcoesSimuladoModel
    {
        public int QuestaoSimuladoId { get; set; }
        public string TextoEnunciadoSimulado { get; set; }
        public string PerguntaSimulado { get; set; }
        public int SimuladoId { get; set; }
        public int DisciplinaSimuladoId { get; set; }
        public List<OpcaoSimuladoModel> OpcoesSimulado { get; set; }
    }  

In the controller action that loads the partial view:

public ActionResult CarregaSimuladosDia1(int codDisciplina)
        {
            IQueryable<PerguntasOpcoesSimuladoModel> simulado = null;

            var subject = _educ365Context.DisciplinasSimulado.Where(d => d.Id == codDisciplina).Select(d => new { NomeDisciplina = d.NomeDisciplina}).FirstOrDefault();
            ViewBag.Disciplina = subject.NomeDisciplina;
            int _simuladoId = int.Parse(HttpContext.Session.GetString("simuladoId"));

            if (_simuladoId.ToString() != null)
            {
                simulado = _educ365Context.QuestoesSimulado
                           .Where(q => q.SimuladoId == _simuladoId && q.DisciplinaSimuladoId == codDisciplina)
                           .Select(q => new PerguntasOpcoesSimuladoModel
                           {
                               QuestaoSimuladoId = q.Id,
                               TextoEnunciadoSimulado = q.TextoEnunciadoSimulado,
                               PerguntaSimulado = q.PerguntaSimulado,
                               SimuladoId = q.SimuladoId,
                               DisciplinaSimuladoId = q.DisciplinaSimuladoId,
                               OpcoesSimulado = q.OpcoesSimulado.Select(o => new OpcaoSimuladoModel
                               {
                                   Id = o.Id,
                                   opcaoResposta = o.opcaoResposta
                               }).ToList()
                           }).AsQueryable();
            }

            return PartialView(simulado);
        }  

And finally my view:

<body>
    <h1>Disciplina: @ViewBag.Disciplina</h1>
    <div class="container">
        <div class="Simulado">
            <h4 style="margin-top:4%;">
                <span class="badge badge-primary">Questões:</span>
            </h4>

            @if (Model != null && Model.Any())
            {
                foreach (var questoes in Model)
                {
                    <div class="BlocoQuestao" style="border: 1px solid #bdbdbd; text-align:justify; border-radius:4px; margin-top:40px; background-color: #f0ffff; padding: 8px;">
                        <div class="Questao" style="padding: 2%;">
                            <span class="badge badge-warning">@string.Format("{0}{1}.", "Q", count)</span>
                            <span id="@string.Format("{0}{1}", "ID_Q", count)" style="display:none;">@questoes.QuestaoSimuladoId</span>
                            <p style="display:inline; padding: 2%;" id="@string.Format("{0}{1}", "Q", count)">@Html.Raw(System.Web.HttpUtility.HtmlEncode(@questoes.TextoEnunciadoSimulado).Replace("\n", "<br />"))<br /><br /> @questoes.PerguntaSimulado </p>
                        </div>


                        <div class="Opcoes" style="margin-left:2%; border:1px solid #bdbdbd; background-color: #bbb9b9;">
                            @foreach (var opcao in questoes.OpcoesSimulado)
                            {
                                <label class="radio-inline">
                                    <input type="radio" name="@string.Format("{0}{1}", "inlineRadioOptions", count)" id="@string.Format("{0}{1}", "inlineRadio", countR)" value="@opcao.opcaoResposta" />@opcao.opcaoResposta
                                </label>
                                <br />
                                countR++;
                            }
                        </div>
                    </div>
                    count++;
                }
                count--;
                <span id="countQuestions" style="display:none;">@count</span>
                <button type="button" class="btn btn-secondary" style="margin-top: 10px;"><i class='fas fa-check pr-1'></i>Finalizar</button>
            }

        </div>
    </div>
</body> 
  • Is the partial view used the same? Unless you duplicate the partial view or save the data somehow I don’t see how you can solve the problem..

  • Because it is @iamdlm actually would like to know how to store this data. And then how to load the page again with the data that was saved and not with the data that loaded the page previously.

No answers

Browser other questions tagged

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