0

I am trying to call MVC action with parameters on html button click. I want to know how can i pass the textbox value as parameter to that action. Following is the code which i am using:

<input type="button" value="Create" onclick="location.href='@Url.Action("Verify", "Login", new { username = "'document.getElementById('txtUsername').value'", password="admin" } )'" />

i want to know the correct method / syntax of how to pass that textbox value

ahmed
  • 13
  • 1
  • 4
  • yes, but their they are passing hard coded parameter values, i want to know how can i pass the corresponding textbox value – ahmed Jul 25 '18 at 20:03

1 Answers1

0

You can do it in JavaScript.

Change your View to

<input type="button" value="Create" onclick="createUser()" />

In the JavaScript:

function createUser() {
    var username = document.getElementById('txtUsername').value;
    var password = "admin";
    $.ajax({
        // do the rest of work here
        type: "POST",
        data: {username: username, password: password },
        url: '@Url.Action("Verify", "Login")',
        async: true,
        success: function (data) {
            // do something
        }
    });
}
Tracy Zhou
  • 714
  • 1
  • 7
  • 11