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?