Lidprogsky
Lidprogsky

Reputation: 43

ASP.NET JavaScript onClick function

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!

Upvotes: 0

Views: 314

Answers (1)

Yahya Altintop
Yahya Altintop

Reputation: 224

$("#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();

}

Upvotes: 1

Related Questions