Set radio button checked item from ASP MVC 3 controller
Solution 1
The answer to this question wound up being incredibly simple. First mistake I made was not using the @Html
controls. Second was using FormCollection
as an input parameter for the index controller. By changing the radio buttons to the following:
@Html.RadioButton("criteria", "bankName", true)<span>Bank Name</span>
@Html.RadioButton("criteria", "EPURL")<span>EPURL</span>
@Html.RadioButton("criteria", "specialNotes")<span>Special Notes</span>
@Html.RadioButton("criteria", "email")<span>Email</span>
@Html.RadioButton("criteria", "dynamicsId")<span>Dynamics ID</span>
@Html.RadioButton("criteria", "stat")<span>Agent ID</span>
and the signature of the Index
method in the controller to this:
public ActionResult Index(string criteria, string searchValue)
The selected radio button stayed selected after the post back.
Solution 2
If I understand your need correctly you could set the name of the item you want checked in the ViewBag (or ViewData or Model) and set the checked property in your view accordingly. Something like this:
<form method="post">
@Html.TextBox("searchValue", ViewBag.CurrentFilter as string, new { placeholder = "Search" })
<input type="image" src="@Url.Content("~/Content/Images/Filter.bmp")" alt="Filter" style="padding-top: 0px;" />
<span class="error" style="clear: both;">
@ViewBag.ErrorMessage
</span>
<a href="#" style="padding-left: 30px;"></a>
<br />
<br />
@{
var checkedItemName = ViewBag.CheckedItemName;
}
<input type="radio" name="criteria" id="bankName" value="bankName" checked="@((checkedItemName == "bankName"))"/>
<label for="bankName">Bank Name</label>
<input type="radio" name="criteria" id="EPURL" value="EPURL" checked="@((checkedItemName == "EPURL"))"/>
<label for="EPURL">EPURL</label>
<input type="radio" name="criteria" id="specialNotes" value="specialNotes" checked="@((checkedItemName == "specialNotes"))"/>
<label for="SpecialNotes">Special Notes</label>
<input type="radio" name="criteria" id="email" value="email" checked="@((checkedItemName == "email"))"/>
<label for="email">Email</label>
<input type="radio" name="criteria" id="dynamicsId" value="dynamicsId" checked="@((checkedItemName == "dynamicsId"))"/>
<label for="dynamicsId">Dynamics ID</label>
<input type="radio" name="criteria" id="stat" value="stat" checked="@((checkedItemName == "stat"))"/>
<label for="fixed">Agent ID </label>
</form>
NealR
Updated on June 05, 2022Comments
-
NealR almost 2 years
Below is a group of radio buttons that appear on my view. I am able to retrieve the selected item via this simple code
string criteria = filter["criteria"];
however I do not know how to retain the selected item. Once the controller posts back to the view the default radio button is always selected.
<form method="post"> @Html.TextBox("searchValue", ViewBag.CurrentFilter as string, new { placeholder = "Search" }) <input type="image" src="@Url.Content("~/Content/Images/Filter.bmp")" alt="Filter" style="padding-top: 0px;" /> <span class="error" style="clear: both;"> @ViewBag.ErrorMessage </span> <a href="#" style="padding-left: 30px;"></a> <br /> <br /> <input type="radio" name="criteria" id="bankName" value="bankName" checked="true"/> <label for="bankName">Bank Name</label> <input type="radio" name="criteria" id="EPURL" value="EPURL" /> <label for="EPURL">EPURL</label> <input type="radio" name="criteria" id="specialNotes" value="specialNotes" /> <label for="SpecialNotes">Special Notes</label> <input type="radio" name="criteria" id="email" value="email" /> <label for="email">Email</label> <input type="radio" name="criteria" id="dynamicsId" value="dynamicsId" /> <label for="dynamicsId">Dynamics ID</label> <input type="radio" name="criteria" id="stat" value="stat" /> <label for="fixed">Agent ID </label> </form>
-
NealR about 11 yearsThis passes the correct value back to the view but for some reason the last radio button is always selected.
-
NealR about 11 yearswhich is beyond bizarre because, if I selecte
EPURL
, this is how that particular radio button is rendered:<input type="radio" name="criteria" id="EPURL" value="EPURL" checked="True"/>