You can do this pretty easily with Jquery and a controller method marked with the HttpPost attribute, your controller method would look something like this:
[HttpPost]
public ActionResult GetSessionVariable()
{
const string sessionVariableName = "MySessionVariable";
var sessionVariable = Session[sessionVariableName] as string;
if (sessionVariable == null)
{
Session[sessionVariableName] = "No";
sessionVariable = "No";
}
return Content(sessionVariable);
}
Obviously your session variable would change elsewhere in your program, this just get's it as it is now, and sets it to a default value if not yet assigned.
Then in your view, you could have a form like so, containing your radio buttons, and an input button which when clicked runs a Javascript method to get the value from the above controller method without refreshing the page:
<form id="MyForm">
<fieldset>
<legend>My Form</legend>
<p>
<label>Radio buttons</label>
<input type="radio" name="radioYesNo" id="radioNo" value="No" checked="checked" />
<label for="radioNo">No</label>
<input type="radio" name="radioYesNo" id="radioYes" value="Yes" />
<label for="radioYes">Yes</label>
<button type="submit" onclick="UpdateRadioBox();">Update Radio Button</button>
</p>
</fieldset>
</form>
The Javascript method for this update is below, it uses the ajax functionality provided by jquery to update the radio button:
<script>
function UpdateRadioBox() {
$("#MyForm").submit(
function () {
var url = "Home/GetSessionVariable";
$.ajax
(
{
type: "POST",
url: url,
success: function (data) {
if (data == "Yes") {
$("#radioYes").attr('checked', 'checked');
} else {
$("#radioNo").attr('checked', 'checked');
}
}
}
);
return false;
}
);
}
</script>
You don't have to run the Javascript under an input button, you can do this however you like (on a timer every x seconds for example).