如何打印ActionResult的结果如何完成

时间:2016-02-18 22:04:55

标签: javascript asp.net ajax asp.net-mvc-4 razor

如何在调用ActionResult后打印结果?

例如,以下是我的观点:

 <a title="Add To Read Later" 
    target="_blank" 
    href="@(Url.Action("Insert", "ReadLater",new {Book_id=Model.Book.Book_id }))">
      <small class="fa fa-2x fa-plus"></small>
 </a>

这是我的ReadLaterController方法:

public ActionResult Insert(int? Book_id)
{
    if (User.Identity.IsAuthenticated)
    {
        var x = User.Identity.GetUserId();
        var result = db.Books.Where(p => p.Book_id == Book_id).Single();
        if(result.Book_id == Book_id)
        {
            var IsExists = db.ReadLaters.FirstOrDefault(t => t.Book_Id == result.Book_id && t.User_Id==x);
            if (IsExists == null)
            {
                ReadLater ReadL = new ReadLater()
                {
                    Book_Id = result.Book_id,
                    User_Id = x.ToString()
                };
                db.ReadLaters.Add(ReadL);
                int state = db.SaveChanges();
                if (state == 1)
                {

                    return new JavaScriptResult { Script = "alert('Successfully Added');" };
                }
            }
            else
            {

                 return new JavaScriptResult { Script = "alert('You Added This book Before');" };

            }
        }
    }

    return new JavaScriptResult { Script = "alert('Not Added');" };
}

如您所见,如果内容已添加到数据库,我想在我调用控制器的同一页面中显示警告,说:如果添加或未添加或已添加的数据取决于方法结果。 ...

现在它可以通过转到方法URL并在没有JavaScript效果的情况下打印结果,就像文本一样:

  

http://localhost:49119/ReadLater/Insert?Book_id=19

该网址的结果是:

  

警告('你之前添加了这本书');

1 个答案:

答案 0 :(得分:1)

这是您可以使用的AJAX示例。我把它作为按钮..它可能是你想要的任何东西。

HTML

    <button data-id="@Model.Book.Book_id" class="insertButton fa fa-2x fa-plus">Add to Read Later</button>

的Ajax

            $('.insertButton').click(function () {

                var myId = $(this).data('id');

                $.ajax({
                    type: "GET",
                    url: '@Url.Action("Insert","ReadLater")?Book_id=' + myId,
                    success: function (response) {
                        alert(response);
                    },
                    failure: function (response) {
                        alert("Failure");
                    }
                });
            });

控制器

[HttpGet]
        public ActionResult Insert(int? Book_id)
        {
            if (User.Identity.IsAuthenticated)
            {
                var x = User.Identity.GetUserId();
                var result = db.Books.Where(p => p.Book_id == Book_id).Single();
                if (result.Book_id == Book_id)
                {
                    var IsExists = db.ReadLaters.FirstOrDefault(t => t.Book_Id == result.Book_id && t.User_Id == x);
                    if (IsExists == null)
                    {
                        ReadLater ReadL = new ReadLater()
                        {
                            Book_Id = result.Book_id,
                            User_Id = x.ToString()
                        };
                        db.ReadLaters.Add(ReadL);
                        int state = db.SaveChanges();
                        if (state == 1)
                        {

                            return Content("Successfully Added");
                        }
                    }
                    else
                    {

                        return Content("You Added This book Before");

                    }
                }
            }

            return Content("Not Added");
        }