自动发布返回MVC4剃须刀页面 [英] Auto postBack in MVC4 razor page

查看:73
本文介绍了自动发布返回MVC4剃须刀页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始在MVC4环境中工作.
当然,我有很多问题.其中之一是使用按钮单击事件.
我可能使用的任何指令,此回发都会破坏我的程序的流程.
因为它会执行我为其他用途放置的任何指令.
我使用"Java脚本"来处理onclick事件,例如:

I start to work in a MVC4 environment.
And of course I have many problems. One of these is the use of button click event.
Any instruction I may use, this post back destroys the flow of my program.
Because it executes any instruction I put for other uses.
I use 'Java Script' to handle the onclick event like that:

 <script type="text/javascript">
 function IntegrityOnClick(status) {
 switch (status) {
 case 1:
 $.ajax({
    type: 'GET',
    url: '@Url.Action("CheckIntegrity_Click", "Models/_mainPage")',
    dataType: 'json'
    });
 case 0 :
    return;
 default:
 }
}

我这种情况引发了我404 not found
的错误 如果我将url:更改为

I this case threw me the error of 404 not found
If I change the url: to

'@Attributes.codeBehind.CheckIntegrity_Click'

然后event可以正常工作,但是也会随着页面加载触发,这不是所需的.
很明显,我的代码格式不正确,但是我不知道这个错误在哪里.
问题是:
有没有办法以正确的方式在我的剃须刀页面上运行button click event? (没有回发干扰).
我已经检查了所有互联网的解决方案,但是发现的内容非常复杂,无法开发.
有什么办法可以帮助我解决这个问题吗?

ADDITION 19/2/19 17:30
在@Marcelo Vismari的协助下,我最终得到了以下代码
首先是按钮.

Then the event works just fine, but it fires with the page load as well, which is not the desired one.
It obvious that somewhere my code isn't so well formed but I don't know where is this mistake.
And the question is:
Is there a way to run button click event on my razor page, in the right way? (without post-back interferes).
I've check all the internet for a solution, but what I found was very complicated, and I can't developed.
Is there any way to assist me on this issue?.

ADDITION 19/2/19 17:30
With the assistance of @Marcelo Vismari I finally end up with the following code
The Button first.

 <button id="checkIntegrity" class="checkIntegrity" onclick="IntegrityOnClick()">

脚本第二.

The Script second.

 <script  type="text/javascript">
    function IntegrityOnClick() {
    // It'll generate an ajax request to IntegrityBtn_Click action, on controller.
    // It's not refresh your page, so will not destroy your flow.
    $.ajax({
        type: 'GET',
        url: '@Url.Action("IntegrityBtn_Click")',
        dataType: 'json',
                });
}
</script>

最后是控制器站点.

And the controller site last.

Public Function IntegrityBtn_Click() As JsonResult
        Return Json(New With {Key Attributes.codeBehind.CheckIntegrity_Click}, JsonRequestBehavior.AllowGet)
    End Function.<br/>

我希望可以帮助任何人在其代码中遇到同样的问题.

I hope to help anybody has facing the same issues on his code.

推荐答案

要控制程序流程,请使用一些使用javascript的ajax调用:

To control your program flow use some ajax calls with javascript:

<input type="button" onclick="myEvent()" />

public class YourController : Controller 
{
    public JsonResult CheckIntegrity_Click() {
        return Json(new { message = "aaa", foo = true }, JsonRequestBehavior.AllowGet));
    }
}

<script>
    function myEvent() {
        // It'll generate an ajax request to CheckIntegrity_Click action.
        // Then it'll return some data back.
        // It's not refresh your page, so will not destroy your flow.
        $.ajax({
            type: 'GET',
            url: '@Url.Action("CheckIntegrity_Click")',
            dataType: 'json',
            success: function(data) {
                // Here is data returned by CheckIntegrity_Click action
                alert(data.message); // aaa
                console.log(data.foo); // true
            }
        });
    }
</script>

这篇关于自动发布返回MVC4剃须刀页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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