如何更改视图而不丢失在mvc中的上一个视图中输入的数据

时间:2019-06-06 17:30:50

标签: c# asp.net asp.net-mvc asp.net-core

我有一个具有两个按钮的视图。在该视图中有一个局部视图,上面装有多个问题的证明(当用户单击一个按钮时,将加载一个学科的证明,而当单击另一个按钮时,将显示另一个学科的证明)。此视图包含问题,每个问题都有单选按钮,带有用于选择正确问题的选项。问题是,当我更改证明时,当我回到上一个证明时,我就错过了我在另一项测试中先前标记的问题。返回上一个测试时如何保持选中单选按钮?

我的模特:

 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; }
    }  

My controller:

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);
        }

My View:

@model IQueryable<Educ365App.Models.PerguntasOpcoesSimuladoModel>

@{
    int count = 1, countR = 0;
}
<head>
    <link rel='stylesheet' href='https://use.fontawesome.com/releases/v5.7.0/css/all.css' integrity='sha384-lZN37f5QGtY3VHgisS14W3ExzMWZxybE1SJSEsQp9S+oqd12jhcu+A56Ebc1zFSJ' crossorigin='anonymous'>
</head>
<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>

0 个答案:

没有答案