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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Manish Kumar ChoudharyPosted Jan 27, 2015, 7:07 AM
Create like this in your view@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>
And in controllerpublic 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();
Niraj SinghPosted Jan 27, 2015, 6:51 AM
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.............
Manish Kumar ChoudharyPosted Jan 27, 2015, 5:47 AM
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" />
}
Manish Kumar ChoudharyPosted Jan 27, 2015, 5:40 AM
then i can give you proper solution .
Niraj SinghPosted Jan 27, 2015, 4:47 AM
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 ChoudharyPosted Jan 27, 2015, 4:40 AM
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 SinghPosted Jan 27, 2015, 4:35 AM
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 ChoudharyPosted Jan 27, 2015, 4:31 AM
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 SinghPosted Jan 27, 2015, 4:26 AM
Niraj SinghPosted Jan 27, 2015, 4:22 AM
Manish Kumar ChoudharyPosted Jan 27, 2015, 4:12 AM