我似乎无法在任何地方找到答案,但我觉得它可能是浏览器特定的(我正在使用Chrome)。为什么当我在确认窗口点击取消时,它会一直弹回来?
<script>
$(document).ready(function() {
$('.postDelete').click(function () {
var c = confirm("Please Confirm Post Deletion");
if (c == true) {
var post = $(event.target).closest('.post');
var postID = post.find('.postID').html();
//Send postID to DeletePost.cshtml to execute the sql delete command
$.ajax({
url: '../DeletePost.cshtml',
type: 'GET',
data: { SelectedPostID: postID },
success: function (result) {
window.location.replace(window.location.href);
console.log("result: " + result);
}
});
}
else { console.log('canceled');}
});
});
</script>
由于它不是引起问题的代码,它必须在我的html中所以这里是: 这是我的_Layout.cshtml
@{
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>@Page.Title</title>
<link rel="stylesheet" type="text/css" href="~/Shared/Template.css" />
<!--JQuery CDN-->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
</head>
<body>
<div id="Header">
<div id="SignIn">
Click me to Sign In
<img id="BiPolarChar" src="~/Resources/Images/BiPolarChar.png" alt="BiPolar Sign In Guy" />
<div id="SignInForm">
@RenderPage("~/Account/_Login.cshtml")
</div>
</div>
<div id="HeaderSlogan">@Page.Title</div>
</div>
<div id="Navigation">@RenderPage("~/Shared/_Navigation.cshtml")</div>
<div id="MainContent"> @RenderBody()</div>
<div id="Footer">
<br />
<span><i>Website designed by Jamie Horton</i></span>
<br /> <br />
<span><i>Last Updated: @(DateTime.Now)</i></span>
@RenderPage("~/Shared/_Google_Analytics.cshtml")
</div>
<!--JQuery-->
<script>
$(document).ready(function () {
console.log('WTF is going on');
@if (WebSecurity.CurrentUserName != "anonymous" & !String.IsNullOrEmpty(WebSecurity.CurrentUserName) & WebSecurity.UserExists(WebSecurity.CurrentUserName) & WebSecurity.IsAuthenticated)
{
<text>
$('.postAuthorized').show();
console.log('Show the dam things');
</text>
}
else
{
<text>
$('.postAuthorized').hide();//Hide authorized post icons
console.log('hide the dam things');
</text>
}
});
</script>
<!--SignIn Form Show/Hide-->
@RenderPage("~/Shared/_SignInForm.cshtml")
<!--Resize Posts-->
@RenderPage("~/Shared/_ResizePosts.cshtml")
</body>
</html>
这是我试图删除帖子的页面:
@{
Layout = "~/Shared/_Layout.cshtml";
Page.Title = "Notes Page";
var db = Database.Open("StarterSite");
try
{
var PagePosts = db.Query("SELECT * FROM Posts WHERE Page='Notes' ORDER BY Date DESC ;");
<div class="posts">
@foreach (var post in PagePosts)
{
@RenderPage("~/Shared/_Posts.cshtml",
new
{
Author = post.Author,
Content = post.Content,
Date = post.Date,
Title = post.Title,
ID = post.ID
})
}
</div>
}
catch { <h1>Sorry but I have reached my maximum connection limit to my database. Please try again. </h1>}
}
@RenderPage("~/Shared/_ResizePosts.cshtml")
这是_Post部分:
@{
<div class="post" id="@PageData["ID"]">
<div class="postTitle">@PageData["Title"]<span class="postID">@PageData["ID"]</span></div>
<div class="postAuthorized">
<div class="postEdit">Edit</div>
<div class="postDelete">Del</div>
</div>
<div class="postByline"> by
<span class="postAuthor">@PageData["Author"]</span> on
<span class="postDate">@PageData["Date"]</span>
</div>
<div class="postContent">
@PageData["Content"]
</div>
</div>
<script>
$(document).ready(function() {
$('.postDelete').click(function () {
var c = confirm("Please Confirm Post Deletion");
if (c == true) {
var post = $(event.target).closest('.post');
var postID = post.find('.postID').html();
//Send postID to DeletePost.cshtml to execute the sql delete command
$.ajax({
url: '../DeletePost.cshtml',
type: 'GET',
data: { SelectedPostID: postID },
success: function (result) {
window.location.replace(window.location.href);
console.log("result: " + result);
}
});
}
else { console.log('canceled');}
});
});
</script>
}
这是DeletePost.cshtml
@functions{
public bool DeletePost()
{
var db = Database.Open("StarterSite");
var postID = Request["SelectedPostID"];
var cmd = "DELETE FROM Posts WHERE ID=" + postID + ";";
try {
//db.Execute(cmd);
return true;
}
catch (Exception er)
{
er.ToString();
}
return false;
}
}
@DeletePost();