question

nathi-mabinza avatar image
0 Votes"
nathi-mabinza asked ZhiLv-MSFT edited

Radio button not passing desired values to my controller

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


dotnet-aspnet-core-razor
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

The default value of a bool is false. Usually the model submitted in the post does not match the action input parameters. This is only guess what problem(s) exist in the code base because you did not share the model or the action parameters.

I recommend the this Learn Razor Pages tutorial to learn how radio buttons work.


0 Votes 0 ·

1 Answer

ZhiLv-MSFT avatar image
0 Votes"
ZhiLv-MSFT answered ZhiLv-MSFT edited

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


12.gif (320.9 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.