Reputation: 1222
I am wanting to check to see which radio button was selected from my Index.cshtml
file and from there I would like to use a switch statement to set attributes to its object. I was reading up on how to do this but majority of them were for groups in a windows form but that would not apply to me since I am using a asp.net core 5.0 web mvc project.
In my Index.cshmtl
I have the following:
....
<form action="@Url.Action("SecIndex", "Second")">
<input type="radio" value="1" /><label>Valid</label>
<input type="radio" value="2" /><label>Wrong</label>
<input type="radio" value="3" /><label>InValid</label>
<input type="submit" value="Address Validation" />
</form>
then in my HomeController
, I have the following method:
[HttpPost]
public IActionResult ValidateAddress()
{
// if radio button was checked, perform the following var request.
var check = ""; // this should be able to grab the label of the button that was selected
switch (check)
{
case "Valid":
var request = new AddressRequest
{
StatusCode = 1,
Address = "2018 Main St New York City, NY, 10001"
};
break;
case "Wrong":
var req = new AddressRequest
{
StatusCode = 2,
Address = "91 Apple City Galveston, TX, 77550"
};
break;
case "Invalid":
var addressRequest = new AddressRequest
{
StatusCode = 3,
Address = "00000 Tree Ln, Miami FL 91041"
};
break;
}
return View();
}
But I cannot seem to figure out how to allow variable check
to perform a check to see which radio button was selected since this is a asp.net core 5.0 web mvc project and not a windows form. Is there a way to do this in this type of project?
UPDATE:
So I realized that I wanted to have the Value as it was before with numbers. So implementing the suggestion, I needed to still redirect the user to the method SecIndex
in my Second
controller. However, I still needed to grab the value of radio button that was selected. So I attempted to do return RedirectToAction("SecIndex");
but when I ran the program and hit the button to validate it gave me:
This page isn't working. If the problem continues, contact the site owner. HTTP ERROR 405
Did I do something incorrectly? the URI shows up as https://localhost:5443/Home/ValidateAddress?Status=1
. Not I did select the first option, which should give me Status as 1. Not sure why it is returning that though.
Inside of my HomeController
:
[HttpPost]
public IActionResult ValidateAddress(string validate)
{
// if radio button was checked, perform the following var request.
switch (validate)
{
case "Valid":
var request = new AddressRequest
{
StatusCode = 1,
Address = "2018 Main St New York City, NY, 10001"
};
break;
case "Wrong":
var req = new AddressRequest
{
StatusCode = 2,
Address = "91 Apple City Galveston, TX, 77550"
};
break;
case "Invalid":
var addressRequest = new AddressRequest
{
StatusCode = 3,
Address = "00000 Tree Ln, Miami FL 91041"
};
break;
}
return RedirectToAction("SecIndex");
}
}
Home View... index.cshtml
:
<form action="@Url.Action("ValidateAddress", "Home")">
<input type="radio" value="1" name="Status"/><label>Valid</label>
<input type="radio" value="2" name="Status"/><label>Wrong</label>
<input type="radio" value="3" name="Status"/><label>InValid</label>
<input type="submit" value="Address Validation" />
</form>
Upvotes: 4
Views: 6206
Reputation: 6638
Give the radio buttons a common name and assign the label text to the value of the radio buttons.
change Index.cshmtl
to :
<form method="post" action="/DemSecondo/ValidateAddress">
<input type="radio" value="Valid" name="myRadio" /><label>Valid</label>
<input type="radio" value="Wrong" name="myRadio" /><label>Wrong</label>
<input type="radio" value="InValid" name="myRadio" /><label>InValid</label>
<input type="submit" value="Address Validation" />
</form>
and change ValidateAddress
action to :
[HttpPost]
public IActionResult ValidateAddress(string myRadio)
{
switch (myRadio)
{
case "Valid":
var request = new AddressRequest
{
StatusCode = 1,
Address = "2018 Main St New York City, NY, 10001"
};
break;
case "Wrong":
var req = new AddressRequest
{
StatusCode = 2,
Address = "91 Apple City Galveston, TX, 77550"
};
break;
case "Invalid":
var addressRequest = new AddressRequest
{
StatusCode = 3,
Address = "00000 Tree Ln, Miami FL 91041"
};
break;
}
return RedirectToAction("SecIndex");
}
Upvotes: 4