After redirect the page, the radio button which i selected earlier should be maintain, How i do it?
Html.RadioButtonFor(model => model.flag, "R", htmlAttributes: new { @id = "oflag1" })
Refusal
Html.RadioButtonFor(model => model.flag, "RJ", htmlAttributes: new { @id = "oflag2" })
Return to JTP
Html.RadioButtonFor(model => model.flag, "RA", htmlAttributes: new { @id = "oflag4" })
Call for Addln.Particulars
Html.RadioButtonFor(model => model.flag, "FC", htmlAttributes: new { @id = "oflag3" })
Forward to Chairman
Html.ValidationMessageFor(model => model.flag, "", new { @class = "text-danger" })

Sachin SinghRank #66 · 26.2K reputationPosted Jan 10, 2024, 3:05 AM EST
To maintain the value you have to do something like this
[HttpPost] public ActionResult YourPostAction(YourModel model) { // Save the selected flag value in TempData TempData["SelectedFlag"] = model.flag; // Perform other actions if needed // Redirect to another action or view return RedirectToAction("YourRedirectedAction"); } public ActionResult YourRedirectedAction() { // Retrieve the selected flag value from TempData var selectedFlag = TempData["SelectedFlag"]?.ToString(); // Create a new model or retrieve an existing one YourModel model = new YourModel { flag = selectedFlag }; return View(model); }@Html.RadioButtonFor(model => model.flag, "R", new { @id = "oflag1", @checked = (Model.flag == "R") }) Refusal @Html.RadioButtonFor(model => model.flag, "RJ", new { @id = "oflag2", @checked = (Model.flag == "RJ") }) Return to JTP @Html.RadioButtonFor(model => model.flag, "RA", new { @id = "oflag4", @checked = (Model.flag == "RA") }) Call for Addln.Particulars @Html.RadioButtonFor(model => model.flag, "FC", new { @id = "oflag3", @checked = (Model.flag == "FC") }) Forward to Chairman @Html.ValidationMessageFor(model => model.flag, "", new { @class = "text-danger" })Sachin SinghRank #66 · 26.2K reputationPosted Jan 10, 2024, 4:38 AM EST
Yes you can do that with JS only also, but that won't be a good approach, here is an example
Note:- You can increase the expiration time from 7 days to any number of days
// Function to set a cookie function setCookie(name, value, days) { var expires = ""; if (days) { var date = new Date(); date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000)); expires = "; expires=" + date.toUTCString(); } document.cookie = name + "=" + value + expires + "; path=/"; } // Function to get a cookie value function getCookie(name) { var nameEQ = name + "="; var ca = document.cookie.split(';'); for (var i = 0; i @Html.RadioButtonFor(model => model.flag, "R", new { @id = "oflag1" }) Refusal @Html.RadioButtonFor(model => model.flag, "RJ", new { @id = "oflag2" }) Return to JTP @Html.RadioButtonFor(model => model.flag, "RA", new { @id = "oflag4" }) Call for Addln.Particulars @Html.RadioButtonFor(model => model.flag, "FC", new { @id = "oflag3" }) Forward to Chairman @Html.ValidationMessageFor(model => model.flag, "", new { @class = "text-danger" })Garima BansalRank #1,555 · 1K reputationPosted Jan 10, 2024, 4:32 AM EST
Can this solution can be resolve by js?
Naimish MakwanaRank #324 · 7.7K reputationPosted Jan 10, 2024, 4:47 AM EST
To maintain the state of a selected radio button after a page redirect, you can use
localStorageorsessionStoragein JavaScript. Here’s a simple example of how you can do it:// Run this when the radio button is clicked $('input[type=radio][name=flag]').change(function() { // Save the selected radio button value in localStorage localStorage.setItem('selectedRadioButton', this.value); }); // Run this when the page loads $(document).ready(function() { // Get the selected radio button value from localStorage var selectedRadioButton = localStorage.getItem('selectedRadioButton'); // If there was a selected radio button saved if (selectedRadioButton) { // Check the saved radio button $('input[type=radio][name=flag][value=' + selectedRadioButton + ']').prop('checked', true); } });This script will save the value of the selected radio button in the user’s browser when a radio button is clicked. Then, when the page is loaded, it will check if there is a saved radio button value and select that radio button.
Please replace
'flag'with the actual name attribute of your radio buttons. Also, make sure to include jQuery in your project for this script to work12.Remember,
localStoragepersists across sessions and will work even if the user closes and reopens their browser, whilesessionStorageis limited to the current session and will be cleared when the user closes their browser. Choose the one that best fits your needs.Please note that this method will not work if the user has disabled web storage in their browser settings. In such cases, you might need to consider server-side solutions.