Is it possible to show/open a View by using Javascript in ASP.net MVC?

Viewed 27

I have an Action Method which returns the View i want to show.

 [HttpPost]
        public ActionResult LoginSuccess(string data)
        {
         
                return View(); 
            
        }

This Action Method is called by a JQuery Post Method. The POST Method sends the values of the two Textboxes, sepeterated by dashes, to the ActionMethod. This works as expected.

<form class='login-form'>
    <div class="flex-row">
        <input id="username" class='lf--input' placeholder='Username' type='text'>
    </div>
    <div class="flex-row">
        <input id="password" class='inpPW' placeholder='Password' type='password'>
    </div>
    <input class='lf--submit' type='submit' onclick="sendData()">
</form>

<script type="text/javascript">
    function sendData() {

        var txtUsername = document.getElementById("username").value;
        var txtPassword = document.getElementById("password").value;
        var fullString = [txtUsername, txtPassword].join('-');
        $.post('/Login/LoginSuccess', { "data": fullString });
    }
</script>

However, when I try click the submit Button, the Action Method is called as expected, but it is not redirecting me to the expected view. Instead, it just adds an Question Mark to the URL and reloads the Site.

I already know its possible just by adding "action="/Login/LoginSuccess" method="post"" to the Forms Tag, but is there a way to use Javascript for this action?

0 Answers
Related