【问题标题】:Redirect to MVC ActionResult with Parameters when DropDownList option is changed更改 DropDownList 选项时重定向到带有参数的 MVC ActionResult
【发布时间】:2017-12-22 10:06:44
【问题描述】:

我正在使用 MVC 创建网站的一部分。在我的一个观点中,我有一个DropDownList。 When a new drop down list option is selected, or in other words onchange, I want my page to be redirected to a specific Controller ActionResult.如果没有参数,我可以到达MyActionActionResult,但是我不知道如何发送所需的参数。

我的控制器动作:

public virtual ActionResult MyAction(int param1, int param2)
{
    return View();
}

我在视图中的下拉列表:

@Html.DropDownList(
        "viewDataItem", Model.MyEnumerableList as SelectList, 
        new { onchange = @"
            var form = document.forms[0];
            form.action='MyAction';
            form.submit();" 
        } )

上面的代码调用了MyAction,但是它没有传入参数。有没有办法以某种方式将参数添加到此代码中?

另一个想法是以某种方式使用@{Response.Redirect(@Url.Action("MyAction", "myController", new { param1 = 2, param2= 3 }));} 作为我的 DropDownList 操作,因为 Response.Redirect 允许我使用参数重定向到 MyAction。有没有办法以某种方式使 onchanged = Response.Redirect?

尝试使 onchange 等于响应,但是当我更改选项时没有任何反应:

@Html.DropDownList(
        "name", Model.MyEnumerableList as SelectList,
        new
        {
            onchange = {Response.Redirect(@Url.Action("MyAction", "controllerName", new { param1 = 5, param2 = 3 }));}
        })

简而言之,当我的 DropDownList 选项更改时,如何调用带有参数的 ActionResult?

herehere 提出了类似的问题,但这些链接中提供的答案都使用 JavaScript,我不知道如何将 JS 与 cshtml 一起使用。我尝试了其中一些答案,但没有一个能解决我的问题。

【问题讨论】:

    标签: c# asp.net-mvc redirect razor


    【解决方案1】:

    您可以在 onchange 事件中指定一个 javascript 函数并在该函数内部:

    var url = '@Html.Raw(Url.Action("MyAction", "controllerName", new { param1=5, param2=2 }))';
    

    然后:

    window.location = url;
    

    最后代码应该是:

    @Html.DropDownList(
        "viewDataItem", Model.MyEnumerableList as SelectList, 
        new { onchange = "SelectionChanged()" 
        } )
    
    <script>
     function SelectionChanged()
     {
      var url = '@Html.Raw(Url.Action("MyAction", "controllerName", new { param1=5, param2=2 }))';
      window.location = url;
     }
    </script>
    

    【讨论】:

    • 我应该在哪里添加window.location = url
    • 在选择改变时将执行的js函数中,所有js代码都将在该函数内
    • 我没有JS代码??你指的是onchanged代码吗?
    • 再次检查我的答案,我把你需要的所有代码都放了。
    • 像这样使用Html.Raw@Html.Raw(Url.Action("MyAction", "controllerName", new { param1=5, param2=2 }))
    【解决方案2】:

    有没有办法以某种方式将参数添加到此代码中?

    当然,有很多方法。其中之一是:

    @Html.DropDownList(
            "viewDataItem", Model.MyEnumerableList as SelectList, 
            new { onchange = @"
                var form = document.forms[0];
                form.action='MyAction?param1=5&param2=3';
                form.submit(); /*Just make sure that form 'method' attribute is set to 'post'*/" 
            } )
    

    但是在你提到的answer 中描述了一个更好的方法。

    有没有办法以某种方式使 onchanged = Response.Redirect?

    不是您尝试使用它的方式。 onchanged 是一个 javascript 事件,而 javascript 对 Response 属性或其他 MVC 服务器端的东西一无所知。

    【讨论】:

      猜你喜欢
      • 2013-02-26
      • 2016-05-04
      • 2012-08-07
      • 1970-01-01
      • 2013-12-28
      • 1970-01-01
      • 1970-01-01
      • 2014-03-01
      • 1970-01-01
      相关资源
      最近更新 更多