How to access @Html.RadioButtonFor in controller
How we can access id of @Html.RadioButtonFor in Controller using MVC4 Razor
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend4days leftRegister
Manish Kumar ChoudharyRank #339 · 7.4K reputationPosted Jan 27, 2015, 2:07 AM EST
@using (Html.BeginForm("Index2", "Testing", FormMethod.Get))
{
// @Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>TestModelh4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.IsQuotedTariff, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<div class="checkbox">
@Html.RadioButtonFor(model => model.IsQuotedTariff,"Yes") <text>Yestext>
@Html.RadioButtonFor(model => model.IsQuotedTariff, "No") <text>Notext>
@Html.ValidationMessageFor(model => model.IsQuotedTariff, "", new { @class = "text-danger" })
div>
div>
div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
div>
div>
div>
public ActionResult Index2(string IsQuotedTariff)
{
if (IsQuotedTariff.Equals("Yes"))
{
//first radio button selected
}
else if (IsQuotedTariff.Equals("NO"))
{
//second radio button selected
}
else
{
//nothing selected.
}
return View();
Manish Kumar ChoudharyRank #339 · 7.4K reputationPosted Jan 26, 2015, 11:12 PM EST
Niraj Singh108 reputationPosted Jan 26, 2015, 11:22 PM EST
Niraj Singh108 reputationPosted Jan 26, 2015, 11:26 PM EST
Manish Kumar ChoudharyRank #339 · 7.4K reputationPosted Jan 26, 2015, 11:31 PM EST
Suppose you are using radio button like
Then you can give the if like above
Or if you don't mention the id separately then default if will be StatusId
Niraj Singh108 reputationPosted Jan 26, 2015, 11:35 PM EST
Here I have to two radioButton like below:-
@Html.RadioButtonFor(m => m.ByTariff, false, new { @class = "radioBut byGlCode", tabindex = 3 })
Calculate GL Code Accommodation Revenue
@Html.RadioButtonFor(m => m.ByTariff,true, new { @class = "radioBut byQuotedTariff", tabindex = 3 })
Use Quoted Tariff
On controller i have to justify that which radioButton is checked
Manish Kumar ChoudharyRank #339 · 7.4K reputationPosted Jan 26, 2015, 11:40 PM EST
Go through this link it will help you
http://www.c-sharpcorner.com/uploadfile/b19d5a/how-to-create-radio-button-in-asp-net-mvc3-razor-application/
Niraj Singh108 reputationPosted Jan 26, 2015, 11:47 PM EST
I have already read this article, Here no controller code is defined which do clear that how we can access checked radiobutton in controller on same group.......
Manish Kumar ChoudharyRank #339 · 7.4K reputationPosted Jan 27, 2015, 12:40 AM EST
then i can give you proper solution .
Manish Kumar ChoudharyRank #339 · 7.4K reputationPosted Jan 27, 2015, 12:47 AM EST
Controller action Method
public string Index(string searchBy)
{
if (searchBy.Equals ("Name"))
{
return "Click On Name Radio Button";
}
else if (searchBy.Equals("Department"))
{
return "Click On Department Radio Button";
}
else
{
return "Click Nothing.";
}
}
View code
@using (Html.BeginForm("Index", "Testing", FormMethod.Get))
{
@Html.RadioButton("searchBy", "Name") Name
@Html.RadioButton("searchBy", "Department") Department
<br />
<input id="btnsubmit" type="submit" value="submit" />
}
Niraj Singh108 reputationPosted Jan 27, 2015, 1:51 AM EST
This is Model Code:
public bool IsQuotedTariff { get; set; }
This is View Code:-
@Html.RadioButtonFor(m => m.IsQuotedTariff, "QuotedTariff", Model.IsQuotedTariff == false ? new { Checked = "checked" } : null)
Use Quoted Tariff
@Html.RadioButtonFor(m => m.IsQuotedTariff, "GlCode", Model.IsQuotedTariff == true ? new { Checked = "checked" } : null)
Calculate GL Code Accommodation Revenue
I have to use this two radiobutton in cotroller to check that either QuotedTariff is on or GLCode
How i can write code to controller to check this.......?
Please do needfull.............