F Msw F Msw - 4 months ago 8
Ajax Question

How To print a result of ActionResult if it is Done

How to print a result after calling ActionResult?

For example, here is my call from a view:

<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>


And here is my
ReadLaterController
method:

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');" };
}


As you see, if the content was added to database, I want to show an alert in the same page from which I called the controller, saying: if the data added or not added or already added depend on the method result ....

Right now it works by going to the method URL and printing the result there without JavaScript effects, just as text:


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


And the result in that URL is:


alert('You Added This book Before');

Answer

Here is an AJAX example you could use. I put it as a button.. It could be whatever you want though.

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");
                    }
                });
            });

Controller

[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");
        }