【发布时间】:2019-11-18 21:15:03
【问题描述】:
我正在尝试在视图中更改下拉值时调用控制器方法“GetTasksList”。然而,在下拉菜单的变化中,控制器的“编辑”方法被调用并将表单提交到数据库。这是不可取的,因为我试图根据下拉值从数据库中获取任务列表。
查看
@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)
{
}
【问题讨论】:
-
这似乎在我创建一个新表单 (localhost:64577/Audit/Create) 时起作用,保存它然后立即编辑它localhost:64577/Audit/Edit。但是,当我稍后编辑它时,URL 是localhost:64577/Audit/Edit/5dcdd8c1d7078b1afd0f28,其中“5dcdd8c1d7078b1afd0f28”是唯一标识符,控件转到 Edit 方法并在下拉值更改时提交表单,并且不获取任务。
标签: c# asp.net-core-mvc onchange html.dropdownlistfor actionresult