Tot*_*Zam 4 c# asp.net-mvc redirect razor
我正在使用MVC创建网站的一部分.在我的一个观点中,我有一个DropDownList.当选择新的下拉列表选项时,或者换句话说onchange,我希望将我的页面重定向到特定的Controller ActionResult.MyAction如果没有参数,我可以到ActionResult,但是我无法弄清楚如何发送所需的参数.
我的控制器动作:
public virtual ActionResult MyAction(int param1, int param2)
{
return View();
}
Run Code Online (Sandbox Code Playgroud)
我在视图中的DropDownList:
@Html.DropDownList(
"viewDataItem", Model.MyEnumerableList as SelectList,
new { onchange = @"
var form = document.forms[0];
form.action='MyAction';
form.submit();"
} )
Run Code Online (Sandbox Code Playgroud)
上面的代码调用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 }));}
})
Run Code Online (Sandbox Code Playgroud)
简而言之,每当我的DropDownList选项被更改时,如何使用参数调用ActionResult?
在这里和这里都提出了类似的问题,但这些链接中提供的答案都使用JavaScript,我不知道如何使用JS和cshtml.我尝试了一些答案,但没有一个能解决我的问题.
小智 7
您可以在onchange事件上指定一个javascript函数并在该函数内部:
var url = '@Html.Raw(Url.Action("MyAction", "controllerName", new { param1=5, param2=2 }))';
Run Code Online (Sandbox Code Playgroud)
然后:
window.location = url;
Run Code Online (Sandbox Code Playgroud)
最后代码应该是:
@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>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10147 次 |
| 最近记录: |