Radio button not passing desired values to my controller

Nathi Mabinza 1 Reputation point
2021-05-25T08:01:02.957+00:00

Hi, I need some assistance on radio button which is not passing value true or false to my controller. It's always false not sure what I'm missing, I can see another values passed through when debugging Job.PublicSubmissionReviewed is always false: PublicSubmissionReviewed is a boolean

<form asp-action="Review">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<input type="hidden" asp-for="Job.Id" />
<div class="form-group">
<p><strong>Approve or Reject public submission</strong></p>
<div class="form-group form-check-inline">
<label class="form-check-label">
<input class="form-radio-input" name="status" value="True" type="radio" asp-for="Job.PublicSubmissionReviewed" /> Approve
</label>
</div>
<div class="form-group form-check-inline">
<label class="form-check-label">
<input class="form-check-input" name="status" value="False" type="radio" asp-for="Job.PublicSubmissionReviewed" /> Reject
</label>
</div>
</div>
<div class="form-row showapprove" id="True">
<div class="form-group col-md-6">
<label asp-for="Job.JobCategoryId" class="control-label"></label>
<select asp-for="Job.JobCategoryId" class="form-control" asp-items="ViewBag.JobCategoryId">
</select>
</div>
<div class="form-group col-md-6">
<label asp-for="Job.Name" class="control-label"></label>
<input asp-for="Job.Name" class="form-control" />
<span asp-validation-for="Job.Name" class="text-danger"></span>
</div>
<div class="form-group col-md-6">
<label asp-for="Job.JobNo" class="control-label"></label>
<input asp-for="Job.JobNo" class="form-control" />
<span asp-validation-for="Job.JobNo" class="text-danger"></span>
</div>
<div class="form-group col-md-6">
<label asp-for="Job.ContractorId" class="control-label"></label>
<select asp-for="Job.ContractorId" class="form-control" asp-items="ViewBag.ContractorId">
</select>
</div>
</div>
<div class="form-group">
<button type="submit" class="btn btn-outline-primary"><i class="fas fa-save" aria-hidden="true"></i> Submit</button>
</div>
</form>

and here is my method to get the values from the form:

public async Task<IActionResult> Review(int id, JobViewModel model)
{
if (id != model.Job.Id)
{
return NotFound();
}

        if (ModelState.IsValid)
        {
            try
            {
                var job = _context.Jobs.Where(j => j.Id == id).SingleOrDefault();

                if (model.Job.PublicSubmissionReviewed == true) // approve public submission
                {
                    job.PublicSubmissionReviewed = true;
                    job.PublicSubmissionReviewDate = DateTime.Now;
                    job.Name = model.Job.Name;
                    job.New = true;
                    job.JobNo = model.Job.JobNo;
                    job.JobCategoryId = model.Job.JobCategoryId;
                    job.ContractorId = model.Job.ContractorId;
                    _context.Update(job);
                    await _context.SaveChangesAsync();

                    return RedirectToAction(nameof(Public));
                }
                else if (model.Job.PublicSubmissionReviewed == false) // reject public submission
                {
                    job.PublicSubmissionReviewed = true;
                    job.PublicSubmissionReviewDate = DateTime.Now;
                    job.New = false;
                    job.PublicSubmissionRejected = true;
                    job.PublicSubmissionRejectedDate = DateTime.Now;
                    job.PublicSubmissionReviewRejectReason = model.Job.PublicSubmissionReviewRejectReason;

                    _context.Update(job);
                    await _context.SaveChangesAsync();

                    return RedirectToAction(nameof(Public));
                }
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!JobExists(model.Job.Id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }
        }
        return View(model);
    }
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,148 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Zhi Lv - MSFT 32,011 Reputation points Microsoft Vendor
    2021-05-26T02:00:54.877+00:00

    Hi @Nathi Mabinza ,

    <input class="form-radio-input" name="status" value="True" type="radio" asp-for="Job.PublicSubmissionReviewed" /> Approve    
    <input class="form-check-input" name="status" value="False" type="radio" asp-for="Job.PublicSubmissionReviewed" /> Reject    
    

    The issue relates the name attribute, since you add the name attribute, in the Post method, you could a parameter named status, then use it to get the selected value. Code like this:

    public async Task<IActionResult> Review(int id, JobViewModel model, bool status)  
    

    If you want to get the radio button selected value via the JobViewModel, you should remove the name attribute.

    Refer the following sample:

    Model:

    public class JobViewModel  
    {  
        public int Id { get; set; }  
        public string Name { get; set; }  
        public Boolean PublicSubmissionReviewed { get; set; }  
    }  
    

    View page:

    @model WebApplication6.Models.JobViewModel      
       
    <div class="row">  
        <div class="col-md-4">  
            <form asp-action="Review">  
                <div asp-validation-summary="ModelOnly" class="text-danger"></div>  
                <input type="hidden" asp-for="Id" />  
                <div class="form-group">  
                    <label asp-for="Name" class="control-label"></label>  
                    <input asp-for="Name" class="form-control" />  
                    <span asp-validation-for="Name" class="text-danger"></span>  
                </div>  
                <div class="form-group form-check-inline">  
                    <label class="form-check-label">  
                        <input class="form-radio-input" value="True" type="radio" asp-for="PublicSubmissionReviewed" /> Approve  
                    </label>  
                </div>  
                <div class="form-group form-check-inline">  
                    <label class="form-check-label">  
                        <input class="form-check-input" value="False" type="radio" asp-for="PublicSubmissionReviewed" /> Reject  
                    </label>  
                </div>  
                <div class="form-group">  
                    <input type="submit" value="Save" class="btn btn-primary" />  
                </div>  
            </form>  
        </div>  
    </div>  
    

    Controller:

    public IActionResult Review()  
    {  
        JobViewModel model = new JobViewModel()  
        {  
            Id = 101,  
            Name = "Job A"  
        };  
        return View(model);  
    }  
    [HttpPost]  
    public IActionResult Review(int id, JobViewModel model)  
    {  
        return View();  
    }  
    

    The result as below:

    99652-12.gif


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,
    Dillion

    0 comments No comments