自动发布返回MVC4剃须刀页面

编程入门 行业动态 更新时间:2024-10-12 05:55:16
本文介绍了自动发布返回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剃须刀页面

本文发布于:2023-11-16 05:17:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1601332.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:剃须刀   页面

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!