xu_my_ 2021-12-30 16:25 采纳率: 100%
浏览 62
已结题

Ajax.BeginForm和Ajax.ActionLink没有反应

问题遇到的现象和发生背景

Ajax没有任何反应,点击发表评论按钮和点赞,ajax没有任何反应,不会弹出回调函数提示,调试的时候发现根本没有触发ajax,不知道为什么

问题相关代码,请勿粘贴截图

视图界面

@model WebWEI.Models.Photograph

@{
    ViewBag.Title = "picDetails";
    Layout = "~/Views/Shared/_Layout.cshtml";
}


<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Details</title>

    <link href="~/Css/global .css" rel="stylesheet" />
    <script src="~/Scripts/ckeditor/ckeditor.js"></script>
    <script src="~/Scripts/ckeditor/config.js"></script>
    <script src="~/Scripts/jquery-3.4.1.js"></script>
    <script src="~/Scripts/jquery.unobtrusive-ajax.js"></script>
    <script src="~/Scripts/jquery.validate.js"></script>
    <script src="~/Scripts/jquery.validate.unobtrusive.js"></script>

</head>

<body>
    <div id="Home">
        <div id="homebox">
            <div id="leftbox">
                <div id="imgbox">
                    @{
                        var item = Model.PhotographUrl;
                    }

                    <img src="@item" style="width:400px;height:300px;" />

                    @*<img class="sample2" data-img2blob="~/Images/我的帖子.png" />*@
                </div>
                <div id="tupianxiangqing">
                    <div style="width:500px;height:50px; padding-left:80px;color:black;margin-top:20px"><span>发布时间:</span>@Html.DisplayFor(model => model.ShootTime)</div>
                    <div style="width: 500px; height: 50px; padding-left: 80px; color: black; margin-top: -20px"><span>图片ID:</span>@Html.DisplayFor(model => model.photographId)</div>
                    <div style="width: 500px; height: 50px; padding-left: 80px; color: black; margin-top: -20px"><span>图片规格:</span>@Html.DisplayFor(model => model.Specifications)</div>
                    <div style="width: 500px; height: 50px; padding-left: 80px; color: black; margin-top: -20px"><span>图片介绍:</span>@Html.DisplayFor(model => model.photographIntroduction)</div>
                    <div style="width: 500px; height: 50px; padding-left: 80px; color: black; margin-top: -20px"><span>图片价格:</span>@Html.DisplayFor(model => model.photographValue)</div>
                </div>
                <div id="xiabox">


                    <div id="button1">
                        <a href='@Url.Action("Download", "Photographs", new { fileName =item})'>
                            立即下载
                        </a>
                        </div>

                        <div id="gou"><button id="button2"> 购物车</button></div>
                    </div>

                </div>
            @{
                WebWEI.Models.post post = ViewBag.Post
                    as WebWEI.Models.post;

                WebWEI.Models.UserInfo user = ViewBag.UserInfo
                    as WebWEI.Models.UserInfo;

            }
            <div id="rightbox">
                <div id="toutitle">
                    @if (user!= null)
                        {
                            string uname = user.userName;
                            string headportrait = user.userHeadPortrait;
                            <div id="touxiang">
                                 
                                <a href="/User/PersonalCenter" target="_blank">
                                    <img src=@headportrait class="headportrait" />
                                </a>
                            </div>
                        }
                    
                    <div id="posttitle">@post.postTitle</div>
                </div>
                <div id="postcontent">@post.postContent</div>
                <div id="zanlunnum">
                    @*<div id="zannum"><span>点赞数</span>@post.likesNums</div>*@

                    <div style="float: left;width: 45px;padding: 5px;font-size: 17px;">
                        @Ajax.ActionLink("点赞", "Carrypost", "User", new { id = post.postId }, new AjaxOptions
                   { HttpMethod = "get", UpdateTargetId = post.postId, OnSuccess = "LikeSuccess" })
                    </div>
                    <div id="@post.postId" style="width: 60px;float: left;font-size: 17px;">
                        @Html.Action("PostZanNum", "User", new { id = post.postId })
                    </div>
                    <div id="lunnum"><span>评论数</span>@post.commentsNums</div>
                </div>
                <div id="lun"></div>
            </div>
        </div>
        <div id="L">
            <div id="pinglun">
                @using (Ajax.BeginForm("AddpostpicComment", "posts", new AjaxOptions
                {
                    HttpMethod = "post",
                    UpdateTargetId = "result",
                    OnSuccess = "Comment"
                }))
                {
                    <input type="hidden" name="postId" id="" value="@Model.postId" />
                    <textarea name="comment" id="editor"></textarea>
                    <script>
                        var editor = CKEDITOR.replace('editor');
                    </script>
                    <br />
                    <input type="submit" value="发表评论" />
                }

            </div>
            <div id="result">
                @Html.Action("PostpicComment", "posts", new { id = Model.postId })
                @*@{Html.RenderPartial("PostpicComment", ViewBag.Comment as IEnumerable<WebWEI.Models.Comment>);}*@
            </div>
        </div>
    </div>

    @*<p>
            @Html.ActionLink("Edit", "Edit", new { id = Model.photographId }) |
            @Html.ActionLink("Back to List", "Index")
        </p>*@
    @section scripts{
       
        <script>
            function Comment(data) {
                if (data == "Login") {
                    alert("请先登录!");
                    window.location.href = '/Home/Login';
                }
                else if (data == "Fault") {
                    alert("评论失败!");
                    window.location.reload();
                }
                else {
                    $("#newcomment").html(data);
                    alert("评论成功!");
                }
            }
            function deletecomment(id) {
                $.ajax({
                    url: "/ShiCi/DeleteShiCiComment",
                    type: "post",
                    data: {
                        commentid: id,
                    },
                    success: function (data) {
                        if (data == "suc") {
                            var comment = document.getElementById(id);
                            comment.style.display = "none";
                            alert("已删除评论!");
                            window.location.reload();
                        }
                        else {
                            alert("删除评论失败!");
                        }
                    }
                });
            }
        </script>
    }

</body>

</html >




运行结果及报错内容

ajax没有反应

我的解答思路和尝试过的方法

可能没有引入相关script

我想要达到的结果
  • 写回答

3条回答 默认 最新

  • CSDN专家-showbo 2021-12-30 17:56
    关注

    题主F12打开浏览器开发工具看控制台是否有报错,确保scripts目录下存在加载的js文件

    <script src="~/Scripts/jquery-3.4.1.js"></script>
        <script src="~/Scripts/jquery.unobtrusive-ajax.js"></script>
        <script src="~/Scripts/jquery.validate.js"></script>
        <script src="~/Scripts/jquery.validate.unobtrusive.js"></script>
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 1月8日
  • 已采纳回答 12月31日
  • 创建了问题 12月30日

悬赏问题

  • ¥15 Linux环境下CA证书更新问题
  • ¥15 sqlserver语句提取结果以外数据
  • ¥60 微信小程序如何上传QQ聊天文件
  • ¥300 开发的系统遭到无良商家的破解,请问如何防止再次发生,并追回损失
  • ¥15 java代码写在记事本上后在cmd上运行时无报错但又没生成文件
  • ¥15 关于#python#的问题:在跑ldsc数据整理的时候一直抱这种错误,要么--out识别不了参数,要么--merge-alleles识别不了参数(操作系统-linux)
  • ¥15 PPOCRLabel
  • ¥15 混合键合键合机对准标识
  • ¥100 现在不懂的是如何将当前的相机中的照片,作为纹理贴图,映射到扫描出的模型上
  • ¥15 安卓OpenCV人脸识别分类器加载