I'm trying to create an ASP.NET MVC application for my completion of course work, using .NET / C# and Razor.
I want to create an application that has a quiz so I get the questions and their alternatives using SQL, sent to a view through TempData
and use two for to go through these structures, like this after the quiz be filled in I need to send the information to a controller.
Basically what I need is to fill the AlternativaEscolhida
with true or false and send it to a controller
Github : https://github.com/joaogqueiroz/PoupaAiMVC
Controller
public IActionResult ValidaRespostas(AvaliacaoModel model)
{
try
{
if (ModelState.IsValid)
{
}
}
catch (Exception e)
{
TempData["Messege"] = "Error" e.Message;
}
return View();
}
View Questionario
@using PoupaAiMVC.Data.Entities
@model PoupaAiMVC.Models.Questionario.AvaliacaoModel
@{
ViewData["Title"] = "Avaliacao";
Layout = "~/Views/Shared/Layout.cshtml";
var questoes = TempData["Questoes"] as List<PoupaAiMVC.Data.Entities.Questao>;
var alternativas = TempData["Alternativas"] as List<PoupaAiMVC.Data.Entities.Alternativa>;
}
<br />
@for (int i = 0; i < questoes.Count(); i )
{
<div class="accordion accordion-flush" id="accordionFlush_@i">
<div class="accordion-item">
<h2 class="accordion-header" id="flush-heading_@i">
<button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#flush-collapse_@i" aria-expanded="false" aria-controls="flush-collapse_@i">
@{int contador = i 1;}
Questão @contador
</button>
</h2>
<div id="flush-collapse_@i" class="accordion-collapse collapse" aria-labelledby="flush-heading_@i" data-bs-parent="#accordionFlushExample">
<div class="accordion-body">@questoes[i].Enunciado</div>
<br />
<br />
@for (int j = 0; j < alternativas.Count(); j )
{
@if (alternativas[j].Id_Questao == questoes[i].IdQuestao)
{
<div>
@Html.RadioButtonFor(m => m.Alternativas[j].IdAlternativa, alternativas[j].AlternativaEscolhida)
@Html.LabelFor(model => model.Questoes[i].Enunciado)
@Html.HiddenFor(model => Model.Alternativas[j].correta)
@Html.HiddenFor(model => Model.Alternativas[j].IdAlternativa)</div>
}
}
</div>
</div>
</div>
}
<div class="d-grid">
<a class="btn btn-primary" href="@Url.Action("ValidaRespostas","Questionario")">Finalizar</a>
</div>
Model AvaliacaoModel
using PoupaAiMVC.Data.Entities;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
namespace PoupaAiMVC.Models.Questionario
{
public class AvaliacaoModel
{
[Required]
public List<Questao> Questoes { get; set; }
[Required]
public List<Alternativa> Alternativas { get; set; }
}
}
Entity Questao
:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PoupaAiMVC.Data.Entities
{
public class Questao
{
public int IdQuestao { get; set; }
public string Enunciado { get; set; }
public int Imagem { get; set; }
public TipoQuestao TipoQuestao { get; set; }
public Nivel Nivel { get; set; }
public List<Alternativa> Alternativas { get; set; }
public static implicit operator List<object>(Questao v)
{
throw new NotImplementedException();
}
}
}
Entity Alternativas
:
public class Alternativa
{
public int IdAlternativa { get; set; }
public string alternativa { get; set; }
public string correta { get; set; }
public int Id_Questao { get; set; }
public bool AlternativaEscolhida { get; set; }
}
CodePudding user response:
in my app . i used this source and work it
public class ReportFormReportFormAccessModel
{
public string Value { get; set; }
public string Text { get; set; }
public bool isRole { get; set; }
public bool Selected { get; set; }
}
<div class="panel-body">
@for (int i = 0; i < Model.ListOfRole.Count; i )
{
<div class="col-md-4">
<div>
<label>
@Html.RadioButtonFor(x => Model.ListOfRole[i].Selected, new { @class = "GroupClass" }) @Model.ListOfRole[i].Text
</label>
@Html.HiddenFor(x => Model.ListOfRole[i].Value)
@Html.HiddenFor(x => Model.ListOfRole[i].Text)
</div>
</div>
}
</div>
[HttpPost]
[ValidateAntiForgeryToken]
public JsonResult MVReportForm(ReportForm model)
{
if (ModelState.IsValid)
{
}
return Json(model, JsonRequestBehavior.AllowGet);
}