0

I am trying to pass 2 values from a view back to the controller. I am having 2 issues:

1- I am successful passing 1 value but I am not sure if my issue is coming from the view or the controller. I am trying to pass the values on the change of a DropDown list. Currently when I make any selection nothing changes. It does use the following URL: http://intranet/HelpDesk?Status=New&Techician=Steve (although for some reason when selecting Technician is clears out the Status) Here is the code of how I am trying: View:

<h>
    Status: @Html.DropDownList("Stat", ViewBag.Stat as SelectList, new { @class = "ddlfilter" })

    Assigned: @Html.DropDownList("Technician", ViewBag.Technician as SelectList, new { @class = "ddlfilter" })


</h>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
    $(function () {
        $(".ddlfilter").change(function () {
            var stat = $("#Stat").val();
            var tech = $("#Technician").val();
            var routeVal = "?Status=";
            var routeVal1 = "Tech=";
            var url = '@Url.Action("Index", "HelpDesk")';
            var link = url + routeVal + stat + "&" + routeVal1 + tech
            window.location.href = link
        });
    })
</script>

Controller:

    public ActionResult Index(string Status, string Tech)
        {
            var helpdesk = from m in db.HelpDesks
                          where m.Status == Status || Status == null || Status == "" &&
                                m.Technician == Tech || Tech == null || Tech == ""
                           select m;

            List<SelectListItem> Stat = new List<SelectListItem>()
            {
                new SelectListItem { Text = "All", Value = "" },
                new SelectListItem { Text = "New", Value = "New" },
                new SelectListItem { Text = "Complete", Value = "Complete" },
            };

            List<SelectListItem> Technician = new List<SelectListItem>()
            {
                new SelectListItem { Text = "All", Value = "" },
                new SelectListItem { Text = "Steve", Value = "Steve" },
                new SelectListItem { Text = "Greg", Value = "Greg" },
            };


            ViewBag.Stat = Stat;
            ViewBag.Technician = Technician;


            return View(helpdesk.ToList());
        }

My second issue is how do I get the DropDownList to stay at the selection once the page reloads?

SEBTulsa
  • 45
  • 6
  • Do you have a route defined for this somewhere? I ask because you said you were having trouble with the second parameter, which is called "Tech", but in your URL it's called "Technician". What if you made the URL querystring "?Status=New&Tech=Steve" so it matches? – Doug F Sep 18 '19 at 17:10
  • OK I should have caught that. So it works now - but depending on with one I select - the other is blank. Every time I select on - they default back to all and all. So if I select Status New when the page refreshes - it shows all the status of new - but it clears out the Tech and Vise Versa - so I am really only getting 1 filter at a time. Does that make sense? How can I get the DropDowns to remember the selection I select after refresh?? – SEBTulsa Sep 18 '19 at 18:08
  • And here are the URLs is uses: Clicking Status: http://localhost:50200/HelpDesk?Status=New&Tech= It is missing the string from whichever DropList I do not choose. Clicking Assigned: http://localhost:50200/HelpDesk?Status=&Tech=Greg – SEBTulsa Sep 18 '19 at 18:30
  • Before I answer that, I do see another typo in your code where you C# code is assigning something to ViewBag.Stat but in your view you're requesting ViewBag.Assigned for the status dropdown, so if that hasn't been corrected already, please do so and see where that gets you. If you're still having issues, please update your code to reflect the fixes so I can help you better. Thanks. – Doug F Sep 18 '19 at 18:43
  • OK. I have updated the code. Thanks! – SEBTulsa Sep 18 '19 at 19:00
  • It looks like to me that whenever you select an item from either one of the dropdown lists, the page is then sent to the index in Helpdesk, but you're not saving those values in the controller to send back to the view. You'll either need to use @Html.DropDownListFor or in your Index method define Stat and Technician as a new SelectList rather than as a list of SelectListItems. Either of those will let you specify the selected item, like in https://stackoverflow.com/questions/624828/asp-net-mvc-html-dropdownlist-selectedvalue – Doug F Sep 18 '19 at 19:19

0 Answers0