Razor 单选按钮不起作用(ASP.NET MVC)

Posted

技术标签:

【中文标题】Razor 单选按钮不起作用(ASP.NET MVC)【英文标题】:Razor radio button don't work (ASP.NET MVC) 【发布时间】:2022-01-10 00:15:50 【问题描述】:

我正在尝试使用 .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; 

【问题讨论】:

按照@marc_s 的要求进行了调整。 【参考方案1】:

在我的应用程序中。我使用了这个来源并使用它

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

【讨论】:

嘿伙计,谢谢,您的解决方案对我有用。

以上是关于Razor 单选按钮不起作用(ASP.NET MVC)的主要内容,如果未能解决你的问题,请参考以下文章

ASP.NET Razor 页面模型绑定不起作用

ASP.NET MVC Razor Url.Action 在 Internet Explorer 8 中不起作用

自动完成在 Visual Studio Code 中的 ASP.NET Core 5 Razor Tag Helper 中不起作用

ASP.NET MVC C# Razor SQL 1 of 2 几乎相同的更新查询不起作用

ASP.NET Core 3.0 控制器路由不起作用

如何在 asp.net 网站中激活 cors (razor 3)