简体   繁体   English

MVC中单选按钮的验证消息

[英]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 我的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();
        }
    }

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 . 由于您使用的是viewmodel,因此我认为最简单和最佳的解决方案是使用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. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM