0

I have an index.cshtml page, in it there is a button:

 <div>
    @Html.EslButton(T("Create Request").ToString(), "createRequestBtn")

</div>

I then have a javascript onclick function in the same page

   $("#createRequestBtn").on('click', function (event) {

        event.preventDefault(event);
    });

In my HomeController.cs I have a function called CreateRequests:

 public ActionResult CreateRequests(ViewModel model)
        {


            return new EmptyResult();

        }

Question

How do I, through clicking the button, go in to the method CreateRequests in Controller?

Thanks!

Lidprogsky
  • 43
  • 8
  • [`preventDefault`](https://developer.mozilla.org/en-US/docs/Web/API/Event/preventDefault) does not take any parameters. It's a good idea to say what type of ASP.NET you're using; Web Forms, MVC, Razor Pages, etc. In this case, it's obvious you're using MVC, but it's nice to to be specific. – Heretic Monkey Jun 04 '21 at 13:00
  • Does this answer your question? [Calling ASP.NET MVC Action Methods from JavaScript](https://stackoverflow.com/questions/8952953/calling-asp-net-mvc-action-methods-from-javascript) – Heretic Monkey Jun 04 '21 at 13:00
  • @HereticMonkey I already got an answer. But thanks anyway! – Lidprogsky Jun 04 '21 at 13:15
  • It doesn't matter if you already got an answer. The question is not about you; it's about the thousands of other people that come after you with the same question. We don't want them to have to sift through 235 questions about how to do this task, when there is one answer on how to do it, and we've already got that answer. Instead, we point them to that one question which has the canonical answer. Closing a question as a duplicate is not punishment! It is a good thing. Your question serves as a signpost for others to find their way to the canonical answer. – Heretic Monkey Jun 04 '21 at 13:21
  • I got you point, but my question might be useful as well? It maybe is similar to the other question, but it is NOT the same, and as new developers people might like to find different answers to the same problem. It might give them a better perspective. Right? – Lidprogsky Jun 04 '21 at 13:24
  • By closing this question @HereticMonkey, you might ruin someones day. Maybe the simple answer they are looking for is in this question? I am talking about people who are new in programing world. – Lidprogsky Jun 04 '21 at 13:26
  • 1
    Closing the question does not remove the question (or the answer). It merely informs people who land on this question from search that the question is answered elsewhere. – Heretic Monkey Jun 04 '21 at 16:17

1 Answers1

1
$("#createRequestBtn").on('click', function (event) {

    event.preventDefault(event);

    var xhr = new XMLHttpRequest();
    xhr.open("post", "/Home/CreateRequests");
    xhr.send();

});

But you should remove model parameter

public ActionResult CreateRequests()
{

   return new EmptyResult();

}
Yahya Altintop
  • 204
  • 2
  • 12