Hi varun3752,
If a CheckBox or RadioButton is checked, then postback values will contain a key-value pair of the form name=value.
If a CheckBox or RadioButton is not checked, then the posted form contains no reference to the CheckBox or RadioButton.
There is no need to use HiddenField.
Refer below sample.
Controller
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Registration(FormCollection form)
{
string email = form["Email"];
string password = form["Password"];
string gender = form["radio-stacked"];
string state = form["State"];
bool rememberMe = form["RememberMe"] != null ? Convert.ToBoolean(form["RememberMe"]) : false;
bool above18 = form["Above18"] != null ? Convert.ToBoolean(form["Above18"]) : false;
return View("Index");
}
}
View
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
</head>
<body>
@using (Html.BeginForm("Registration", "Home", FormMethod.Post))
{
<div class="container">
<form class="col-sm-10">
<div class="form-group row">
<label for="staticEmail" class="col-sm-2 col-form-label">Email</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="staticEmail" name="Email">
</div>
</div>
<div class="form-group row">
<label for="inputPassword" class="col-sm-2 col-form-label">Password</label>
<div class="col-sm-10">
<input type="password" class="form-control" id="inputPassword" name="Password">
</div>
</div>
<div class="form-group row">
<label for="inputPassword" class="col-sm-2 col-form-label">Male</label>
<div class="col-sm-10">
<div class="custom-control custom-radio">
<input type="radio" class="custom-control-input" id="customControlValidation2" name="radio-stacked" value="Male" required>
<label class="custom-control-label" for="customControlValidation2"></label>
</div>
</div>
</div>
<div class="form-group row">
<label for="inputPassword" class="col-sm-2 col-form-label">Female</label>
<div class="col-sm-10">
<div class="custom-control custom-radio mb-3">
<input type="radio" class="custom-control-input" id="customControlValidation3" name="radio-stacked" value="Female" required>
<label class="custom-control-label" for="customControlValidation3"></label>
<div class="invalid-feedback">More example invalid feedback text</div>
</div>
</div>
</div>
<div class="form-group row">
<label class="col-sm-2 col-form-label">State</label>
<div class="col-sm-10">
<select class="form-control" name="State">
<option>State 1</option>
<option>State 2</option>
<option>State 3</option>
<option>State 4</option>
</select>
</div>
</div>
<div class="form-group row">
<label for="inputPassword" class="col-sm-2 col-form-label">Remember me</label>
<div class="col-sm-10">
<div class="custom-control custom-checkbox mb-3">
<input type="checkbox" class="custom-control-input" id="customControlValidation1" name="RememberMe" value="true" required>
<label class="custom-control-label" for="customControlValidation1"></label>
</div>
</div>
</div>
<div class="form-group row">
<label for="inputPassword" class="col-sm-2 col-form-label">Above 18 years</label>
<div class="col-sm-10">
<div class="custom-control custom-switch">
<input type="checkbox" class="custom-control-input" id="customSwitch1" name="Above18" value="true">
<label class="custom-control-label" for="customSwitch1">Yes/No</label>
</div>
</div>
</div>
<button class="btn btn-primary text-center" type="submit">Submit form</button>
</form>
</div>
}
</body>
</html>