I work on asp.net mvc I face issue selected value All requests cleared after display result data and back to Pending Request
so my issue on selected value option if i select option All requests it display all requests success but drop down selected option display as Pending Request
so How to solve issue selected option clear after display data
action GetSelectedDropDownChanged on controller WorkforceRequests hit and catched when make debug and detect value 1 if i select pending request and 2 if i select all request
but issue when select option All request it back to option Pending Request so How to prevent this behaviour from happen
code details
Pending Request
All requestson controller WorkforceRequestsController
public async Task GetSelectedDropDownChanged() //Assuming key in your dropdown is string
{
string selectedValue = Request.Form["statusselect"].ToString();
List workforceRequest = new List();
if (selectedValue == "1")
{
workforceRequest = Workforce.GetPendingOnPalnningSection(RoleKey.PLS);
}
else
workforceRequest = Workforce.GetAllRequestsOnPalnningSection(RoleKey.PLS);
return View(workforceRequest);
}
Amit MohantyRank #112 · 20K reputationPosted Jul 17, 2023, 1:58 AM EDT
I think the issue lies in the way the selected value is being handled in the controller. Currently, the selected value is not being passed back to the view after the form submission, which is causing the dropdown to revert to its default value upon displaying the result data. Check the below code this may help you:
public async Task GetSelectedDropDownChanged(string statusselect) { List workforceRequest = new List(); if (statusselect == "1") { workforceRequest = Workforce.GetPendingOnPalnningSection(RoleKey.PLS); } else if (statusselect == "2") { workforceRequest = Workforce.GetAllRequestsOnPalnningSection(RoleKey.PLS); } // Pass the selected value back to the view var model = new YourModelClass { WorkforceRequests = workforceRequest, SelectedOption = statusselect }; return View(model); }ahmed salahRank #2,240 · 628 reputationPosted Jul 17, 2023, 3:02 AM EDT
thanks for support
but Model.SelectedOption
I don't have any property on Model name selectedoption
so what i do to add make that please
Amit MohantyRank #112 · 20K reputationPosted Jul 17, 2023, 3:43 AM EDT
If you don't have a property named SelectedOption in your existing model, you can create a separate class to hold the selected option value or can use the ViewBag, ViewData. So here an example using ViewBag.
[HttpPost] public async Task GetSelectedDropDownChanged(string statusselect) { List workforceRequest = new List(); if (statusselect == "1") { workforceRequest = Workforce.GetPendingOnPalnningSection(RoleKey.PLS); } else if (statusselect == "2") { workforceRequest = Workforce.GetAllRequestsOnPalnningSection(RoleKey.PLS); } // Store the selected option value in the ViewBag ViewBag.SelectedOption = statusselect; return View(workforceRequest); }ahmed salahRank #2,240 · 628 reputationPosted Jul 17, 2023, 5:07 AM EDT
thanks for support issue still exist
it refresh page and after refresh go to first item
ful code
function Onchange() { document.getElementById("up").click(); } Select Status Pending Request All requests[HttpPost] public async Task GetSelectedDropDownChanged() //Assuming key in your dropdown is string { string selectedValue = Request.Form["statusselect"].ToString(); List workforceRequest = new List(); if (selectedValue == "1") { workforceRequest = Workforce.GetPendingOnPalnningSection(RoleKey.PLS); } if(selectedValue=="2") { workforceRequest = Workforce.GetAllRequestsOnPalnningSection(RoleKey.PLS); } ViewBag.SelectedOption = selectedValue; return View(workforceRequest); }from debug funtion it detect as viewbage have value 2 but it got first item 0 as select status
so how to solve this issue
Amit MohantyRank #112 · 20K reputationPosted Jul 17, 2023, 5:32 AM EDT
try this