如何检查哪个事件导致了 ASP.NET MVC 应用程序中的回发
我的 ASP.net MVC 应用程序中有一个视图和一个控制器。
我有一个“提交”按钮&在我看来,只有一个“取消”按钮。
当我单击下面的任何按钮时,都会触发操作。
AcceptVerbs(HttpVerbs.Post)
public ActionResult Result()
{
}
现在的问题是我如何知道在我的 public ActionResult Result() 中,回发是否是由于“提交”或“取消”引起的?
I have one View and one Controller in my ASP.net MVC Application.
I have one "Submit" button & one "Cancel" button in my view.
When I click any of the botton below action will be triggered.
AcceptVerbs(HttpVerbs.Post)
public ActionResult Result()
{
}
Now the Question is how i will know in my public ActionResult Result() , whether post back caused because of "Submit" or "Cancel" ??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你可以尝试像
you can try like
您可以通过以下步骤解决该问题
:在视图的表单中放置一个名为“取消”的隐藏字段。为其指定默认值false。
二.在取消按钮的 onclick 事件中,添加以下脚本:
将“myForm”更改为表单的名称。
三.将 Result() 的方法签名更改为以下内容:
通过模型绑定,可以在操作方法的签名中访问隐藏字段“cancel”的值。
You could solve the problem with the following steps:
i. Place a hidden field called 'cancel' in the form on your view. Give it a default value of false.
ii. In the onclick event for your cancel button, add the following script:
Change 'myForm' to be the name of your form.
iii. Change the method signature for Result() to the following:
Through model binding, the value of your hidden field 'cancel' will be accessible in the signature of your action method.
感谢您的回答。
我得到的另一个解决方案是,
Thanks for the answers.
One more solution I got is ,
我建议您的“取消”按钮不应提交表单。有多种方法可以处理此问题,但最简单的方法是在导航回上一页的按钮上使用 javascript onclick 事件。 (
history.go(-1)
) 如果这不能满足您的需求,您还可以指定要导航到的特定路线。最复杂的是拦截客户端的点击并决定点击哪条路线,但我通常只在我想在表单上有多个提交按钮来点击不同的操作时才这样做,这是一种相当罕见的情况。I would suggest that your "cancel" button should not submit the form. There are several ways to handle this, but the simplest would be to have a javascript onclick event on the button that navigates back to the previous page. (
history.go(-1)
) If that doesn't meet your needs, you can also specify a specific route to navigate to. Most complex would be to intercept the click on the client side and decide which route to hit, but I typically only do that if I want to have multiple submit buttons on the form that will hit different actions, and that's a fairly rare situation.