将参数从Vew传递到控制器

时间:2019-09-18 16:55:01

标签: javascript asp.net-mvc

我试图将2个值从一个视图传递回控制器。 我有2个问题:

1-我成功传递了1值,但不确定我的问题是来自视图还是控制器。我正在尝试在DropDown列表的更改上传递值。目前,当我进行任何选择时,都不会更改。它确实使用以下URL:http://intranet/HelpDesk?Status=New&Techician=Steve(尽管出于某些原因选择Technician时会清除状态) 这是我正在尝试的代码: 查看:

<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>

控制器:

    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());
        }

我的第二个问题是,在页面重新加载后,如何使DropDownList保持选择状态?

0 个答案:

没有答案