Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
138 views
in Technique[技术] by (71.8m points)

asp.net - 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");
    }
}
question from:https://stackoverflow.com/questions/65545956/how-to-post-in-net-core-mvc

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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");
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...