更改DropDownList选项时,使用参数重定向到MVC ActionResult [英] Redirect to MVC ActionResult with Parameters when DropDownList option is changed

查看:115
本文介绍了更改DropDownList选项时,使用参数重定向到MVC ActionResult的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用MVC创建网站的一部分.在我的一种视图中,我有一个DropDownList.当选择了一个新的下拉列表中选择,或者换句话说,我希望我的网页被重定向到一个特定的控制器.如果没有参数,我可以进入MyAction ActionResult,但是我不知道如何发送所需的参数.

I am using MVC to create part of a website. In one of my Views I have a 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. I am able to get to MyAction ActionResult if there are no parameters, however I can't figure out how to send in the needed parameters.

我的控制器操作:

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

我在视图中的DropDownList:

My DropDownList in View:

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

上面的代码调用MyAction,但是它不发送参数.有没有办法以某种方式将参数添加到此代码中?

The above code calls MyAction, however it does not send in the parameters. Is there a way to somehow add the parameters to this code?

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

Another thought was to somehow use @{Response.Redirect(@Url.Action("MyAction", "myController", new { param1 = 2, param2= 3 }));} as my DropDownList action since Response.Redirect allows me to redirect to MyAction with parameters. Is there a way to somehow make onchanged = Response.Redirect?

尝试使onchange等于响应,但是当我更改选项时什么也没发生:

The tried making onchange equal the response, but the nothing happens when I change my option:

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

简而言之,只要更改了DropDownList选项,如何调用带参数的ActionResult?

In short, how do I call an ActionResult with parameters whenever my DropDownList option is changed?

此处

Similar questions were asked here and here, but the answers provide in those links all use JavaScript and I don't know how to use JS with cshtml. I tried some of those answers, but none of them solved my problems.

推荐答案

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

You can specify on the onchange event a javascript function and inside that function:

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>

这篇关于更改DropDownList选项时,使用参数重定向到MVC ActionResult的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆