使用Ajax的MVC ActionLink如何正确使用return false [英] MVC ActionLink with Ajax How to use return false correctly
问题描述
我有一个简单的Ajax.ActionLink(使用Asp.Net MVC 4)
我的场景是:当用户点击提交按钮时,我打开一个对话框 - 确认框,其中有确定或取消按钮。如果用户单击确定按钮,则Ajax成功并使用window.location.reload()重新加载窗口。如果用户单击取消按钮;没有任何反应,代码返回实际的页面。
我的问题是:当我点击提交按钮时,对话框确认框打开但代码就像点击确定按钮即使我没有单击对话框确认的确定按钮,我的页面重新加载。我认为使用return false有问题
我的问题是:我如何正确使用return false?作为一个脚本新手,所有的帮助将不胜感激。
我有一个像这样的Ajax动作链接:
I have a simple Ajax.ActionLink(Using Asp.Net MVC 4)
My scenerio is: When user clicks submit button, i open a "dialog-confirm" box and which has "Ok" or "Cancel" buttons. If user clicks Ok button Ajax is success and reload the window with "window.location.reload()" If user clicks "Cancel" button; nothing happens and code returns the actual page.
My problem is: When i click the submit button, "dialog-confirm" box opens but code works like clicking like "Ok" button even i don't click the dialog-confirm's "Ok" button and my page reloads. I think i have a problem with using "return false"
My question is: How can i use return false properly? As a script newbie, all your helps will be appreciated.
I have an Ajax actionlink like this:
@Ajax.ActionLink("Send", "Action", "Controller", new { myID= Model[i].myID},
new AjaxOptions
{
OnBegin = "gg",
OnSuccess="aa"
}
,
new
{
@class = "button",
id = Model.[i].myID,
style = "margin-right:8px;font-weight: bold"
})
这是我的脚本:
Here is my script:
<script type="text/javascript">
function gg(e) {
var url = $(this).attr('href');
var myId= $(this).attr("id");
$.ajax({
url: "/Controller/Action/",
type: "POST",
cache: false,
dataType: "json",
data: { term: myId},
success: function (myId) {
$("#bura").html(myId);
$("#dialog-confirm").dialog({
data: myId,
autoOpen: false,
resizable: true,
height: 300,
width: 350,
show: { effect: 'drop', direction: "up" },
modal: true,
draggable: true,
buttons: {
"OK": function () {
$(this).dialog("close");
window.location = url;
return true;
}, "Cancel": function () {
$(this).dialog("close");
return false;
},
}
});
$("#dialog-confirm").dialog('open');
return false;
}
})
};
function aa(e) { window.location.reload(); };
</script>
<div id="dialog-confirm" style="display: none">
<p>
Send my values.
</p>
</div>
推荐答案
(this).attr('href');
var myId =
(this).attr('href'); var myId=
(this).attr(id);
(this).attr("id");
.ajax({
url: / Controller / Action /,
类型:POST,
cache:false,
dataType:json,
data:{term:myId},
成功:函数(myId){
.ajax({ url: "/Controller/Action/", type: "POST", cache: false, dataType: "json", data: { term: myId}, success: function (myId) {
这篇关于使用Ajax的MVC ActionLink如何正确使用return false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!