user3431310
user3431310

Reputation: 871

Validation Message for radio button in MVC

Currently I am creating online form, so I have a issue with validating radio button field. I managed to create validation message for all my textbox, but it seems like creating validation message for radiobutton is way different than creating validation for textbox?

My Problem is
I have radio button for gender, "Male" and "Female", how should I validate these field?

My ViewModel

    public class PersonalValidator
{
    public int personalInfoId { get; set; }

    [Required(ErrorMessage="Gender is required")]
    public string gender { get; set; }

    public string occupation { get; set; }
    public Nullable<int> maritalId { get; set; }
    public Nullable<int> registrationId { get; set; }
    public Nullable<int> eduInfoId { get; set; }
    public Nullable<int> monthlyId { get; set; }
}

My Razor

        <tr>
        <td> Gender</td>
        <td colspan="2">
            @Html.RadioButtonFor(model => model.personalValid.gender, "Male") Male
            @Html.ValidationMessageFor(model => model.personalValid.gender)

            @Html.RadioButtonFor(model => model.personalValid.gender, "Female") Female
            @Html.ValidationMessageFor(model => model.personalValid.gender)
        </td>
<tr>

My Controller

         [HttpPost]
    public ActionResult TestValidation(RegisterInfoPA viewmodel)
    {
        using (var database = new TMXEntities())
        {
            if(ModelState.IsValid)
            {
                var personalVM = viewmodel.personalValid;                

                     //save personal info
                    personalInfo personalDB = new personalInfo();

                    personalDB.gender = personalVM.gender;                                       
                    personalDB.occupation = personalVM.occupation;
                    personalDB.maritalId = personalVM.maritalId;
                    personalDB.eduInfoId = personalVM.eduInfoId;
                    personalDB.monthlyId = personalVM.eduInfoId;

                    db.personalInfoes.Add(personalDB);
                    db.SaveChanges();

                   return RedirectToAction("SuccessfullyCreated");

            }

            return View();
        }
    }

Upvotes: 1

Views: 7724

Answers (1)

user3643092
user3643092

Reputation: 436

I know it is a bit late but it might help someone else. Since you are using viewmodel, I think the easy and best solution is by using AddModelError.

In Your Controller

[HttpPost]
public ActionResult TestValidation(RegisterInfoPA viewmodel)
{
    using (var database = new TMXEntities())
    {
       //VALIDATE FIRST
       MainValidation(viewmodel)

       //Proceed to process data
       -----------codes------------------           
    }
}

   public void MainValidation(RegisterInfoPA validationData)
    {
            if (validationData.personalValid.gender == null)
            {
                ModelState.AddModelError("gender", "Please select gender");
            }

    }

Hope this helps.

Upvotes: 3

Related Questions