0

So I have a .ASP MVC Web Application project. I want to run a void method from the controller class when I press a button using AJAX. No variable input or output data needed. I just want to create a pdf file and save it on my local machine.

Right now, nothing at all happens when I click the button. I don't think the ajax script works, 0 connection. This is my Controller method:

    [HttpPost]
    public void Test()
    {
        string dok = System.IO.File.ReadAllText("C:\\Users\\axel\\Desktop\\Repo\\Cert\\employee_regular.html");
        var Renderer = new IronPdf.HtmlToPdf();
        var HtmlTemplate = dok;

        var Pdf = Renderer.RenderHtmlAsPdf(HtmlTemplate);
        Pdf.SaveAs("C:\\Users\\axel\\Desktop\\Repo\\Cert\\Arbetsgivarintyg_vanlig_heltid.pdf");

    }

This is my Index.cshtml file

@{
    ViewBag.Title = "Home Page";
}


<div class="row">
    <div class="col-md-12">
        <h2>Request employement certificate</h2>




       <input type="button" onclick="BtnClick()" value="Click me" />



    </div>
</div>
<script>
    function BtnClick() {
        $ajax({
            url: "/Home/Test",
            method: "POST",
            success: function () {
                alert("ok");
            },
            error: function () {
                alert("not ok")
            }
        })
    }

</script>

Really happy for any help

3
  • Are there any errors on the browser's development console? When you use the browser's debugging tools, is the network request made for the AJAX call? What is the server's response? Commented Mar 10, 2020 at 1:31
  • Hello, no errors at all, there is nothing happening. It doesnt execute anything when I press the button Commented Mar 10, 2020 at 1:44
  • You forgot the (dot) after "$". $.ajax. Besides that, everything works fine. Commented Mar 10, 2020 at 9:28

1 Answer 1

0

Well there can be several reasons why your code is not working.

  1. First Make sure you are actually able to make a call to a function, Just simply add simple alert message before calling the ajax and see if the alert triggers.

  2. The second thing is to validate url replace the hardcoded url and add url using URL helper.

  3. I would recommend you to make a function as JsonResult Instead of Void, because an exception can happen when creating pdf. [This change is optional but I do recommend it]

So after all the changes your code would look something like this

@{
      ViewBag.Title = "Home Page";
    }


    <div class="row">
      <div class="col-md-12">
        <h2>Request employement certificate</h2>

        <input type="button" onclick="BtnClick()" value="Click me" />

      </div>
    </div>
    <script>
      function BtnClick() {
        $ajax({
          alert("Funciton is working"); // change confirm function is working
          url: "@Url.Action("Test","Home")", // change using Url Helper to create valid URL
          method: "POST",
          success: function (data) {
             if (data == true)
                {
                  alert("pdf created sucessfully ok");
                }
             else
                {
                 alert("exception happend when creating pdf not ok");
                }

          },
          error: function () {
            alert("not ok")
          }
        })
      }

</script>

Your Back End would look something like this

[HttpPost]
public JsonResult Test()

{

    try {
        string dok = System.IO.File.ReadAllText("C:\\Users\\axel\\Desktop\\Repo\\Cert\\employee_regular.html");
        var Renderer = new IronPdf.HtmlToPdf();
        var HtmlTemplate = dok;

        var Pdf = Renderer.RenderHtmlAsPdf(HtmlTemplate);
        Pdf.SaveAs("C:\\Users\\axel\\Desktop\\Repo\\Cert\\Arbetsgivarintyg_vanlig_heltid.pdf");
        return Json(true, JsonRequestBehavior.AllowGet);
    }
    catch(Exception ex) {
        return Json(false, JsonRequestBehavior.AllowGet);
    }

}
Sign up to request clarification or add additional context in comments.

1 Comment

It says (JS) Identifier expected on alert("Funciton is working"); and it gives "Index:42 Uncaught ReferenceError: BtnClick is not defined at HTMLInputElement.onclick" in chrome developer tools. Any tips?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.