c# - At least one radio button should be checked through custom validation in asp.net mvc -


i new mvc framework. have 2 radio buttons , want @ least 1 should checked. want through custom validation, need help?

the field "usworkpermit":

my view code:

do have work visa ? @html.radiobuttonfor(m => m.usworkpermit,"true") yes have @html.radiobuttonfor(m => m.usworkpermit, "false") no don't 

my model code:

[usworkpermitvalidation] public boolean usworkpermit { get; set; } 

my controller code:

[httppost] public actionresult index(models.jobapplication jobapplication) {     if (modelstate.isvalid)     {     }     return view(); } 

i created custom validator incomplete. want ask how can force @ least 1 radio button checked ??

public class usworkpermitvalidation : validationattribute {     protected override validationresult isvalid(object value, validationcontext validationcontext)     {         // check @ least 1 radio button checked.     } } 

how values of each radio buttons on custom validator? if know then can checking ??

why wasting time when people have wasted time make easy us. try using unabtrusive js instead.

class usworkpermitvalidation {   [required(errormessage = "your error message")]   public boolean usworkpermit { get; set; } } 

in view.cshtml

@html.label("yes, i've") @html.radiobuttonfor(model => model.usworkpermit, "yes") @html.label("no, don't have") @html.radiobuttonfor(model => model.usworkpermit, "no") 

and add reference of following jquery scripts files within bundle/current view.

<script src="~/scripts/jquery-3.1.1.min.js"></script> <script src="~/scripts/jquery.validate.js"></script> <script src="~/scripts/jquery.validate.unobtrusive.min.js"></script> 

here snap of run.

enter image description here


Comments

Popular posts from this blog

php - Permission denied. Laravel linux server -

google bigquery - Delta between query execution time and Java query call to finish -

python - Pandas two dataframes multiplication? -