How to POST in .Net Core MVC

i have a login page with Username and Password inputs and also a submit button. I want to call a method in my Controller that checks if the data matches and shows the result. how do i call that method? I tried the following but it didnt work it passes those as parameters

        @using (Html.BeginForm("Logmein", "LoginController", FormMethod.Post))
    {
        <form method="post">
            <div class="input-group mt-3">
                <div class="input-group-prepend">
                </div>
                <input type="text" class="form-control" placeholder="Username" asp-for="Userusername">
            </div>

            <div id="login_usernametxt" class="input-group mt-2">

                <input id="login_passwordtxt" type="password" class="form-control" placeholder="Password" asp-for="Userpassword">
            </div>

            <button id="login_signinbtn" class="mt-3 btn bg-warning text-light" style="float: right;" formmethod="post">Sign in</button>

        </form>
    }

here is my controller.
    public class LoginController : Controller
{
    public LoginInfo Logininformation { get; set; }

    
    public IActionResult Userlogin()
    {
        return View();
    }

    [HttpPost]
    public IActionResult Logmein()
    {
        //checks if the data matches.... 
        return RedirectToPage("/Home/Index");
    }
}

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Tyr to include model with following properties Userusername,Userpassword in Logmein() method

Also u can remove html helper method ( @using (Html.BeginForm(“Logmein”, “LoginController”, FormMethod.Post)))
Ex:

<form method = "post" asp-controller = "Login" asp-action = "Logmein" >
            <div class="input-group mt-3">
                <div class="input-group-prepend">
                </div>
                <input type="text" class="form-control" placeholder="Username" asp-for="Userusername">
            </div>

            <div id="login_usernametxt" class="input-group mt-2">

                <input id="login_passwordtxt" type="password" class="form-control" placeholder="Password" asp-for="Userpassword">
            </div>

          
        <input type = "submit" value = "Login" /> 

        </form>

Model Class
public class Login
{
   
    public string Userusername { get; set;  }
     public string Userpassword  { get; set;  }
}

Controller Method:
[HttpPost]
    public IActionResult Logmein(LoginModel login)
    {
        //checks if the data matches.... 
        return RedirectToPage("/Home/Index");
    }


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x