C# 为什么此操作运行两次?
这是一个发布评论的操作。出于某种原因,该评论被发布了两次。当我放置一个断点时,我看到当它到达这个操作的底部时,它又回到了开始!我不明白为什么C# 为什么此操作运行两次?,c#,asp.net-mvc,C#,Asp.net Mvc,这是一个发布评论的操作。出于某种原因,该评论被发布了两次。当我放置一个断点时,我看到当它到达这个操作的底部时,它又回到了开始!我不明白为什么 [HttpPost] public ActionResult postComment(string comment, string userId, string workerid) { CleanerManager cm = new CleanerManager("Cleaning_LadyConnectionString"); Comme
[HttpPost]
public ActionResult postComment(string comment, string userId, string workerid)
{
CleanerManager cm = new CleanerManager("Cleaning_LadyConnectionString");
CommentsOnUser c = new CommentsOnUser();
c.Comment = comment;
c.CleanerId = int.Parse(workerid);
c.UserId = int.Parse(userId);
c.Date = DateTime.Today;
cm.AddCommentOnUser(c);
return this.RedirectToAction
("Profile", new { id = workerid });
}
下面是javascript
$(".hiredButton").on('click', function () {
$("#commentModal").modal();
$(".postComment").on('click', function () {
var comment = $("#Message").val();
var workerId = $(".postComment").data('workerid');
var userId = $(".postComment").data('userid');
$.post("/S/postComment", { comment: comment, userId: userId, workerId: workerId }, function () {
window.location = "http://baltimoresitter.com/S/profile?Id=" + workerId;
});
});
});
这里是风景
<button type="button" data-workerid="@Model.Cleaner.id" data-userid="@Model.User.id" class="btn btn-default postComment" data-dismiss="modal">Post Comment</button>
发表评论
每次单击按钮时,另一个事件正在注册到“.postComment”上,因此该行
$(".postComment").on('click' ...
每次运行时都会在单击事件上注册另一个函数,您会看到它被多次调用。这可能是您视图中的某个内容。我认为,问题在于视图或某些脚本。用户是否在其上发表评论?这可能是由于客户端事件重复导致的监视客户端上的网络流量以验证发送到服务器的请求数。如果您看到额外的请求,可以双击submit按钮或运行额外时间的JavaScript,跟踪提交表单时发生的情况,以查看是否发生了任何奇怪的事情。同时,创建一个!。我添加了javascipt,@dlatkay,我正在测试它。它没有被按下两次。