【问题标题】:How to invoke my post method when I'm changing dropdown list in ASP.NET MVC当我在 ASP.NET MVC 中更改下拉列表时如何调用我的 post 方法
【发布时间】: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


    【解决方案1】:

    首先,在DropDownList 上使用location.href 似乎是错误的:

    @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 的简写版本,默认使用 POST 提交方法。

    相关问题:

    Autopost back in mvc drop down list

    MVC 4 postback on Dropdownlist change

    【讨论】:

      猜你喜欢
      • 2011-02-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-16
      • 2017-03-15
      • 1970-01-01
      相关资源
      最近更新 更多