【问题标题】:@Html.DropDownListFor onchange is not calling the mentioned Action method@Html.DropDownListFor onchange 没有调用提到的 Action 方法
【发布时间】:2019-11-18 21:15:03
【问题描述】:

我正在尝试在视图中更改下拉值时调用控制器方法“GetTasksList”。然而,在下拉菜单的变化中,控制器的“编辑”方法被调用并将表单提交到数据库。这是不可取的,因为我试图根据下拉值从数据库中获取任务列表。

enter image description here

查看

 @Html.DropDownListFor(x => x.AuditDecision, (SelectList)ViewBag.ListofAuditDecision, "Please select", new { @class = "form-control input-10", id = "AuditStatus", onchange = "testing();", @style = "width: 300px;" })

<script>
  function testing() {
        alert("in testing");
        document.forms[0].action = 'GetAuditTasksList';
       // window.location.href = 'GetAuditTasksList';
        document.forms[0].submit();
    }
</script>

控件转到下面的方法

 [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(Audit audit)
        {
 //submitting to database
}

而不是

[HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult GetAuditTasksList(Audit audit)
        {
}

【问题讨论】:

标签: c# asp.net-core-mvc onchange html.dropdownlistfor actionresult


【解决方案1】:

您应该使用辅助方法为表单生成正确的操作 uri。通过这种方式,您可以显式传递任何必需的参数,并且可以防止将来对路由规则进行任何更改。例如;

    document.forms[0].action = '@Url.Action("GetAuditTasksList", ... )';

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-01
    • 2016-02-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-01
    • 1970-01-01
    • 2011-10-01
    相关资源
    最近更新 更多