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

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 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器