当我在 ASP.NET MVC 中更改下拉列表时如何调用我的 post 方法
How to invoke my post method when I'm changing dropdown list in ASP.NET MVC
我是 MVC 和 Javascript 的新手,所以请耐心等待,我正在开发小型应用程序,当我需要 select 下拉列表中的某些内容时,我就加入了并且基于 selection 我需要将用户重定向到另一个视图,我还需要以某种方式确定我应该将用户重定向到哪里,所以这就是为什么我也尝试传递参数(数据库 ID 到我的 post 方法)但不幸的是这不起作用,在下面的部分中我将 post 我的代码:
将数据发送到我的 DropDownList 的方法:
public ActionResult ShowArticleGroup()
{
List<ArticleGroup> articlesGroups = GroupsController.GetAllGroups();
ViewBag.articlesGroups = articlesGroups;
return View(articlesGroups);
}
[HttpPost]
public ActionResult ShowArticleGroup(string id)
{
//Here I wanted to take ID of selected Group and because there will be allways 3 Groups I can do if else and Redirect by ID
if(id =="00000000-0000-0000-0000-000000000002")
{
return RedirectToAction("Create","Article");
}
return RedirectToAction("Create", "Article");
}
我的 VIEW - 视图上只有一个控件:只有一个下拉菜单,根据 selection,我应该被重定向到另一个视图,我想在这里获取 [=23] 的 ID =]ed 组,然后我想将用户重定向到适当的视图:
@model IEnumerable<Model.ArticleGroup>
@{
ViewBag.Title = "Add new article";
}
<h3 style="text-align:center">Choose article group</h3>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<hr />
@Html.ValidationSummary(true)
<div class="form-group" style="text-align:center">
@Html.DropDownList("Group", new SelectList(ViewBag.articlesGroups, "GroupID", "GroupTitle.Name"), null, new { onchange = "document.location.href = '/Articles/ShowArticleGroup/' + this.options[this.selectedIndex].value;" })
</div>
</div>
}
首先,location.href
在 DropDownList
上的用法在这里似乎是错误的:
@Html.DropDownList("Group", new SelectList(ViewBag.articlesGroups, "GroupID", "GroupTitle.Name"), null,
new { onchange = "document.location.href = '/Articles/ShowArticleGroup/' + this.options[this.selectedIndex].value;" })
AFAIK,location.href
用于使用 HTTP GET 重定向到另一个页面,因此它将尝试调用第一个 ShowArticleGroup
不带参数的操作方法,并且URL 参数被忽略,因为给定的 URL 参数只存在于 POST.
要使用 DropDownList
提交表单,您需要将 change
事件触发 POST 处理到控制器操作方法中:
jQuery
<script type="text/javascript">
$(document).ready(function() {
$("#Group").change(function() {
var groupId = $("#Group").val();
$.post('@Url.Action("ShowArticleGroup", "ControllerName")', { id: groupId }, function (response, status) {
// response handling (optional)
});
});
});
</script>
下拉列表
@Html.DropDownList("Group", new SelectList(ViewBag.articlesGroups, "GroupID", "GroupTitle.Name"), null)
如果您想在表单提交期间传递视图模型内容,我建议您使用强类型 DropDownListFor
绑定到视图模型方法。
注意:$.post
是 $.ajax
的 shorthand 版本,默认使用 POST 提交方法。
相关问题:
Autopost back in mvc drop down list
MVC 4 postback on Dropdownlist change
我是 MVC 和 Javascript 的新手,所以请耐心等待,我正在开发小型应用程序,当我需要 select 下拉列表中的某些内容时,我就加入了并且基于 selection 我需要将用户重定向到另一个视图,我还需要以某种方式确定我应该将用户重定向到哪里,所以这就是为什么我也尝试传递参数(数据库 ID 到我的 post 方法)但不幸的是这不起作用,在下面的部分中我将 post 我的代码:
将数据发送到我的 DropDownList 的方法:
public ActionResult ShowArticleGroup()
{
List<ArticleGroup> articlesGroups = GroupsController.GetAllGroups();
ViewBag.articlesGroups = articlesGroups;
return View(articlesGroups);
}
[HttpPost]
public ActionResult ShowArticleGroup(string id)
{
//Here I wanted to take ID of selected Group and because there will be allways 3 Groups I can do if else and Redirect by ID
if(id =="00000000-0000-0000-0000-000000000002")
{
return RedirectToAction("Create","Article");
}
return RedirectToAction("Create", "Article");
}
我的 VIEW - 视图上只有一个控件:只有一个下拉菜单,根据 selection,我应该被重定向到另一个视图,我想在这里获取 [=23] 的 ID =]ed 组,然后我想将用户重定向到适当的视图:
@model IEnumerable<Model.ArticleGroup>
@{
ViewBag.Title = "Add new article";
}
<h3 style="text-align:center">Choose article group</h3>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<hr />
@Html.ValidationSummary(true)
<div class="form-group" style="text-align:center">
@Html.DropDownList("Group", new SelectList(ViewBag.articlesGroups, "GroupID", "GroupTitle.Name"), null, new { onchange = "document.location.href = '/Articles/ShowArticleGroup/' + this.options[this.selectedIndex].value;" })
</div>
</div>
}
首先,location.href
在 DropDownList
上的用法在这里似乎是错误的:
@Html.DropDownList("Group", new SelectList(ViewBag.articlesGroups, "GroupID", "GroupTitle.Name"), null,
new { onchange = "document.location.href = '/Articles/ShowArticleGroup/' + this.options[this.selectedIndex].value;" })
AFAIK,location.href
用于使用 HTTP GET 重定向到另一个页面,因此它将尝试调用第一个 ShowArticleGroup
不带参数的操作方法,并且URL 参数被忽略,因为给定的 URL 参数只存在于 POST.
要使用 DropDownList
提交表单,您需要将 change
事件触发 POST 处理到控制器操作方法中:
jQuery
<script type="text/javascript">
$(document).ready(function() {
$("#Group").change(function() {
var groupId = $("#Group").val();
$.post('@Url.Action("ShowArticleGroup", "ControllerName")', { id: groupId }, function (response, status) {
// response handling (optional)
});
});
});
</script>
下拉列表
@Html.DropDownList("Group", new SelectList(ViewBag.articlesGroups, "GroupID", "GroupTitle.Name"), null)
如果您想在表单提交期间传递视图模型内容,我建议您使用强类型 DropDownListFor
绑定到视图模型方法。
注意:$.post
是 $.ajax
的 shorthand 版本,默认使用 POST 提交方法。
相关问题:
Autopost back in mvc drop down list
MVC 4 postback on Dropdownlist change