【发布时间】:2017-04-26 19:48:13
【问题描述】:
我对 MVC 和 Javascript 非常陌生,所以请耐心等待,我正在开发小型应用程序,当我需要从下拉列表中选择某些内容并根据该选择重定向用户时,我开始参与其中到另一个视图,我还需要以某种方式确定我应该将用户重定向到哪里,这就是为什么我也尝试传递参数(数据库 ID 到我的 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 - 视图上只有一个控件:只有一个下拉菜单,根据选择,我应该被重定向到另一个视图,我想在这里获取所选组的 ID,然后我想重定向用户适当的看法:
@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>
}
【问题讨论】:
标签: javascript asp.net-mvc controller views