why not display text Required in Html after submit radio button empty?

1.It delivers when you press the answer in the first question why ?

2.not display text Required in Html after submit radio button empty in span tag.

<span asp-validation-for="@item.CheckAns" class="text-danger"></span>

views/Homecontroller1/Index
This is CSHTML

@model List<QuestionCommentViewModel>

@{
    ViewData["Title"] = "Index";
}


<div class="container">

    <div class="row">
        <div class="col-lg-12">

            <form asp-action="Index" method="post">
                @foreach (var item in Model)
                {
                    <h3>@item.OneQuestions.TextQ</h3>

                    @foreach (var r in item.ListChoices)
                    {



                        <input type="radio" id="@r.Id" name="@r.Qid" asp-for="@item.CheckAns" value="@r.TextCh">
                        <label for="@r.Id">@r.TextCh</label><br>

                        <span asp-validation-for="@item.CheckAns" class="text-danger"></span>
                    }

                }
                <input type="submit" value="submit" style="background-color:dodgerblue;Color:white; border-radius:5px; font-weight:bold;" />

            </form>

        </div>

    </div>

</div>
@section Scripts {
    @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}

model

using System.Threading.Tasks;
using Task222SchoolApp.Models;

namespace Task222SchoolApp.Models
{
    public class QuestionCommentViewModel
    {
        public Question OneQuestions { get; set; }
        public IQueryable<Choice> ListChoices { get; set; }
        [BindProperty, Required(ErrorMessage = "Please choose Answer.")]
        public virtual string CheckAns { get; set; }


    }
}

controller

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Threading.Tasks;
using Task222SchoolApp.Models;


namespace WebApplication6.Controllers
{
   
    public class HomeController1 : Controller

    {

        private readonly SchoolContext _context;
        public HomeController1(SchoolContext context)
        {
            _context = context;
        }
        // GET: HomeController1
        public IActionResult Index()
        {
            List<QuestionCommentViewModel> QCVM = new List<QuestionCommentViewModel>();

            QCVM= GetQCVM();
          //  Console.WriteLine("qqqqqqq");
         
            return View(QCVM);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public IActionResult Index(List<QuestionCommentViewModel> QCVM)
        {
            if (ModelState.IsValid)
            {

            }
            return View();
        }
        private IQueryable<Question> getQustion()
        {
            var one = _context.Questions.Include(q => q.QidNavigation);
           
            
             return one;
        }
         private List<QuestionCommentViewModel> GetQCVM()
         {
            Console.WriteLine("111222222222222222222");
            var ListQuestion = getQustion();

           List<QuestionCommentViewModel> QCVM=new List<QuestionCommentViewModel> ();

            foreach (var item in ListQuestion)
            {
                var ch = _context.Choices.Include(q => q.QidNavigation).Where(q=>q.Qid==item.Id);
                 QCVM.Add(new QuestionCommentViewModel() { OneQuestions = item, ListChoices= ch });
                //  return QCVM;

            }

           
            return QCVM;
    }
   
        // GET: HomeController1/Details/5
        public ActionResult Details(int id)
        {
            return View();
        }

        // GET: HomeController1/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: HomeController1/Create
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(IFormCollection collection)
        {
            try
            {
                return RedirectToAction(nameof(Index));
            }
            catch
            {
                return View();
            }
        }

        // GET: HomeController1/Edit/5
        public ActionResult Edit(int id)
        {
            return View();
        }

        // POST: HomeController1/Edit/5
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(int id, IFormCollection collection)
        {
            try
            {
                return RedirectToAction(nameof(Index));
            }
            catch
            {
                return View();
            }
        }

        // GET: HomeController1/Delete/5
        public ActionResult Delete(int id)
        {
            return View();
        }

        // POST: HomeController1/Delete/5
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Delete(int id, IFormCollection collection)
        {
            try
            {
                return RedirectToAction(nameof(Index));
            }
            catch
            {
                return View();
            }
        }
    }
}

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You need to change your code like below

@foreach (var item in Model)
            {
                <h3>@item.OneQuestions.TextQ</h3>

                @foreach (var r in item.ListChoices)
                {
                    <input type="radio" id="@r.Id" asp-for="@item.CheckAns" value="@r.TextCh">
                    <label for="@r.Id">@r.TextCh</label><br>

                }
                <span asp-validation-for="@item.CheckAns" class="text-danger"></span>

            }

Test Result:

why not display text Required in Html after submit radio button empty?


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x