【发布时间】:2012-05-25 11:39:54
【问题描述】:
【问题讨论】:
标签: asp.net-mvc-3 button actionmethod
【问题讨论】:
标签: asp.net-mvc-3 button actionmethod
萨钦,
如果您使用的是 jquery(您没有提及,但我会展示它,因为它与 mvc 相当标准),您将执行以下操作:
$('#buttonId').click(function(){
document.location = '@Url.Action("MyAction","MyController")';
});
当然,您可能想要使用 ajax,但这是一个基本示例。
【讨论】:
您可以使用 html <form>:
@using (Html.BeginForm("SomeAction", "SomeController", FormMethod.Get))
{
<input type="submit" value="Click me" />
}
【讨论】:
你是怎么做到的? 与不使用 MVC 的方式相同。
<INPUT TYPE="BUTTON" VALUE="Home Page" ONCLICK="window.location.href='/Controller/Action'">
【讨论】:
除了 Darin 关于使用表单 GET 方法的回答外,您还可以调用 javascript 函数,然后依次调用操作。
您可以在单击时拦截按钮单击事件并运行自己的代码。该代码可以使用 Ajax 异步调用操作,或者只是简单地导航到操作方法
这里是拦截按钮点击事件的示例 javascript
$(document).ready(function () {
myButton.onclick = function (event) {
// in here you can call an ajax method or just navigate to an action
return false;
}
// or using jQuery
$('#myButton').click(function (e) {
// do whatever here
e.preventDefault;
});
});
或者你可以拦截一个有 href 属性的按钮
$(function () {
$("#myButton").click(function () {
var href = $(this).attr("href");
var route = href + "?paramName=" + $('#SomeValue').val();
$(this).attr("href", route);
});
});
这会添加您可能已存储在页面上另一个输入中的参数信息,并将其附加到 Url,然后导航到操作
【讨论】: