在 MVC4 razor 页面中自动回发
Auto postBack in MVC4 razor page
我开始在 MVC4 环境中工作。
当然,我有很多问题。其中之一就是按钮点击事件的使用。
我可能使用的任何指令,这个 post 返回破坏了我的程序的流程。
因为它执行我为其他用途而设置的任何指令。
我使用 'Java Script' 来处理 onclick
事件:
<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:
更改为
'@Attributes.codeBehind.CheckIntegrity_Click'
然后 event
工作得很好,但它也会随着页面加载而触发,这不是我们想要的。
很明显,我的代码在某处格式不太好,但我不知道这个错误在哪里。
问题是:
有没有办法以正确的方式在我的剃须刀页面上 运行 button click event
? (没有 post-back 干扰)。
网上找了个解决办法,但是发现很复杂,开发不出来
有什么办法可以帮助我解决这个问题吗?
19/2/19 添加 17:30
在@Marcelo Vismari 的帮助下,我最终得到了以下代码
首先是按钮。
<button id="checkIntegrity" class="checkIntegrity" onclick="IntegrityOnClick()">
第二个脚本。
<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>
最后是控制器站点。
Public Function IntegrityBtn_Click() As JsonResult
Return Json(New With {Key Attributes.codeBehind.CheckIntegrity_Click}, JsonRequestBehavior.AllowGet)
End Function.<br/>
我希望能帮助任何在他的代码中遇到同样问题的人。
要控制您的程序流程,请使用 ajax 调用 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 环境中工作。
当然,我有很多问题。其中之一就是按钮点击事件的使用。
我可能使用的任何指令,这个 post 返回破坏了我的程序的流程。
因为它执行我为其他用途而设置的任何指令。
我使用 'Java Script' 来处理 onclick
事件:
<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:
更改为
'@Attributes.codeBehind.CheckIntegrity_Click'
然后 event
工作得很好,但它也会随着页面加载而触发,这不是我们想要的。
很明显,我的代码在某处格式不太好,但我不知道这个错误在哪里。
问题是:
有没有办法以正确的方式在我的剃须刀页面上 运行 button click event
? (没有 post-back 干扰)。
网上找了个解决办法,但是发现很复杂,开发不出来
有什么办法可以帮助我解决这个问题吗?
19/2/19 添加 17:30
在@Marcelo Vismari 的帮助下,我最终得到了以下代码
首先是按钮。
<button id="checkIntegrity" class="checkIntegrity" onclick="IntegrityOnClick()">
第二个脚本。
<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>
最后是控制器站点。
Public Function IntegrityBtn_Click() As JsonResult
Return Json(New With {Key Attributes.codeBehind.CheckIntegrity_Click}, JsonRequestBehavior.AllowGet)
End Function.<br/>
我希望能帮助任何在他的代码中遇到同样问题的人。
要控制您的程序流程,请使用 ajax 调用 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>