selected value option All Request clear after display value How to prevent this behavior from happen ?

Ahmed Abd El Aziz 315 Reputation points
2023-07-16T22:38:06.83+00:00

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

<form action="/WorkforceRequests/GetSelectedDropDownChanged"  method="post">
            <select class="form-control" id="statusselect" name="statusselect">
                <option value="1">Pending Request</option>
                <option value="2">All requests </option>
            </select>
        </form>

on controller WorkforceRequestsController


        public async Task<ActionResult> GetSelectedDropDownChanged() //Assuming key in your dropdown is string
        {
            string selectedValue = Request.Form["statusselect"].ToString();
            List<WorkforceRequest> workforceRequest = new List<WorkforceRequest>();
            if (selectedValue == "1")
            {


          workforceRequest = Workforce.GetPendingOnPalnningSection(RoleKey.PLS);


            }
            else

                workforceRequest = Workforce.GetAllRequestsOnPalnningSection(RoleKey.PLS);

            return View(workforceRequest);

        }
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,417 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Lan Huang-MSFT 28,841 Reputation points Microsoft Vendor
    2023-07-17T06:54:35.69+00:00

    Hi @Ahmed Abd El Aziz,

    I think I understand what you need.

    What you're saying should be that when the form is submitted, the form resets in preparation for another data entry session. So it will come back to the option "Pending Request".

    You can set a value in TempData to keep the selected value, and retrieve TempData in the View page to set the DropDownList value using JavaScript.

    <form action="/WorkforceRequests/GetSelectedDropDownChanged" method="post">
        <select class="form-control" id="statusselect" name="statusselect">
            <option value="1">Pending Request</option>
            <option value="2">All requests </option>
        </select>
        <input type="submit" value="Submit" />
        <script src="~/Scripts/jquery-3.4.1.min.js"></script>
        
        @if (TempData["statusselect"] != null)
        {
            <script type="text/javascript">
                    $(function () {
                        $("#statusselect").val(@TempData["statusselect"]);
                    });
            </script>
        }
    </form>
    
    [HttpPost]
            public async Task<ActionResult> GetSelectedDropDownChanged() //Assuming key in your dropdown is string
            {
                string selectedValue = Request.Form["statusselect"].ToString();
                List<WorkforceRequest> workforceRequest = new List<WorkforceRequest>();
               //.........
                TempData["statusselect"] = selectedValue;
                return View(workforceRequest);
    
            }
    

    9

    Best regards,

    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments