Ajax没有击中控制器动作

时间:2018-04-03 20:39:31

标签: html ajax asp.net-mvc view controller

尝试将answercomments保存到数据库中 这是我的html表单,

<div id="replyform" class="card my-4 d-none">
    <h5 class="card-header">Cevap Yaz:</h5>
    <div class="card-body">
        <form>
            <div class="form-group">
                <textarea id="replytext" name="replytext" typeof="text" class="form-control" rows="3"></textarea>
            </div>
            <button type="submit" id="sendreply" name="sendreply" class="btn btn-primary">Cevap Yaz</button>
        </form>
    </div>
</div>

这是我的homecontroller动作代码

 public JsonResult ReplyComment(string replycomment, int articleid,int commentid)
    {
        var UserId = Session["UserId"];
        if (replycomment == null)
        {
            return Json(true, JsonRequestBehavior.AllowGet);
        }
        db.ReplyComments.Add(new ReplyComment
        { UserId = Convert.ToInt32(UserId), ArticleId = articleid, CommentId=commentid, Paragraph = replycomment,  Date = DateTime.Now });
        db.SaveChanges();
        return Json(false, JsonRequestBehavior.AllowGet);
    }

我的ajax代码在这里

<script type="text/javascript">

 $(document).ready(function () {

    $("#sendreply").click(function () {

        var r_comment = $("#replytext").val();
        var r_commentid = $(".astar").val();

        $.ajax({
            cache: false,
            url: '@Url.Action("ReplyComment","Home")',
            contentType: "application/json; charset=utf-8",
            data: { replycomment: r_comment, articleid:@Model.ArticleId, commentid: r_commentid },
            type: 'POST',
            dataType: 'json',
            success: function (data) {
            alert("İşlemOkey");
            }
        });
});
})

我无法使用此代码调用Replycomment操作

1 个答案:

答案 0 :(得分:0)

我已经测试了这段代码,它会对你有用。因为我是通过ajax发布的,所以我已经从html的主体中删除了表单标签。

对于后端(php),我只是通过发送变量r_comment和r_commentid来测试它,它工作正常。它现在应该与您的后端代码一起使用。如果它适合你,请给我一个喊叫............

<html>
<head>
<body>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script>

         $(document).ready(function(){

          $("#sendreply").click(function () {



       var r_comment = $("#replytext").val();
       var r_commentid = $(".astar").val();
       var articleid = '20';
/* lets test with data

        var r_comment ='my comment';
        var r_commentid = '20';

        */
        $('#loader').fadeIn(400).html('Data is being processed');


         // assuming that you want query result by posting a variable
         var datasend = "r_comment="+ r_comment  + "&r_commentid=" + r_commentid  + "&articleid=" + articleid;

         $.ajax({

         type:'POST',
         url:'smoke1.php',
         data:datasend,
         crossDomain: true,
         cache:false,
         success:function(msg){

//display image loader or text to alert the use that content is being processed

         $('#loader').hide();

// and display result
alert("İşlemOkey");

         $('#result').fadeIn('slow').prepend(msg);




         }

         });

      })

         });


    </script>


<div id="replyform" class="card my-4 d-none">
    <h5 class="card-header">Cevap Yaz:</h5>
    <div class="card-body">



            <div class="form-group">
               replytext: <textarea id="replytext" name="replytext" class="form-control" rows="3"></textarea>
            </div>

<div class="form-group">
                astar: <input type="text" class="astar">
            </div>



<div id="loader"></div>
<div id="result"></div>
<br>


            <button type="submit" id="sendreply" name="sendreply" class="btn btn-primary">Cevap Yaz</button>

    </div>
</div>


</body></html>

我在php中测试后端(用你的后端代码试试)

<强> smoke1.php

<?php
echo "ok";
echo $_POST['r_comment'];
echo $_POST['r_commentid'];
echo $_POST['articleid'];
?>