Razor 单选按钮不起作用(ASP.NET MVC)
Razor radio button don't work (ASP.NET MVC)
我正在尝试使用 .NET/C# 和 Razor 创建一个 ASP.NET MVC 应用程序来完成我的课程作业。
我想创建一个有测验的应用程序,所以我使用 SQL 获取问题及其替代方案,通过 TempData
发送到视图并使用两个 for 来浏览这些结构,像这样在完成测验后我需要将信息发送给控制器。
基本上我需要的是用 true 或 false 填充 AlternativaEscolhida
并将其发送到控制器
Github : https://github.com/joaogqueiroz/PoupaAiMVC
控制器
public IActionResult ValidaRespostas(AvaliacaoModel model)
{
try
{
if (ModelState.IsValid)
{
}
}
catch (Exception e)
{
TempData["Messege"] = "Error" + e.Message;
}
return View();
}
查看问卷
@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>
型号 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; }
}
}
实体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();
}
}
}
实体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; }
}
在我的应用程序中。我使用了这个来源并开始工作
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);
}
我正在尝试使用 .NET/C# 和 Razor 创建一个 ASP.NET MVC 应用程序来完成我的课程作业。
我想创建一个有测验的应用程序,所以我使用 SQL 获取问题及其替代方案,通过 TempData
发送到视图并使用两个 for 来浏览这些结构,像这样在完成测验后我需要将信息发送给控制器。
基本上我需要的是用 true 或 false 填充 AlternativaEscolhida
并将其发送到控制器
Github : https://github.com/joaogqueiroz/PoupaAiMVC
控制器
public IActionResult ValidaRespostas(AvaliacaoModel model)
{
try
{
if (ModelState.IsValid)
{
}
}
catch (Exception e)
{
TempData["Messege"] = "Error" + e.Message;
}
return View();
}
查看问卷
@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>
型号 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; }
}
}
实体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();
}
}
}
实体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; }
}
在我的应用程序中。我使用了这个来源并开始工作
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);
}