3

I'm trying to POST a form to my controller the same way that I used to do with ASP.NET MVC 5 but it's not working.

The controller:

[HttpGet]
public IActionResult Login()
{
    return View();
}

[HttpPost]
public async Task<IActionResult> Login([FromBody] Login LoginModel)
{
    return View();
} 

And HTML

<form asp-action="Login" asp-controller="Home" method="post" class="mt-4">
    <div class="row">
        <div class="col-lg-12">
            <div class="form-group">
                <label class="text-dark" for="uname">E-mail</label>
                <input class="form-control" id="uname" type="text"
                       placeholder="digite e-mail">
            </div>
        </div>
        <div class="col-lg-12">
            <div class="form-group">
                <label class="text-dark" for="pwd">Senha</label>
                <input class="form-control" id="pwd" type="password"
                       placeholder="digite sua senha">
            </div>
        </div>
        <div class="col-lg-12 text-center">
            <button  type="submit" class="btn btn-block btn-dark" />
        </div>
    </div>
</form>

I tried to use the helper:

@using (Html.BeginForm("Login", "Login", FormMethod.Post))
{
}

But the HTML generated doesn't pass the controller path :

enter image description here

Startup.cs

public void ConfigureServices(IServiceCollection services)
{
    services.AddRazorPages().AddRazorRuntimeCompilation();
    services.AddControllersWithViews();
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    app.UseHttpsRedirection();
    app.UseStaticFiles();

    app.UseRouting();
    app.UseAuthorization();

    app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller=Home}/{action=Login}/{id?}");
        });
}
4
  • 1
    it should work. try using this: <button asp-controller="Home" asp-action="Index"> Commented Mar 20, 2020 at 0:37
  • I did, the same thing! not working! Commented Mar 20, 2020 at 0:43
  • Do you have any tags in your class name? can you try adding [Route("Speaker/{id:int}")] to the method? Commented Mar 20, 2020 at 0:45
  • Since that's your default route, there shouldn't be a need to post explicitly to /Home/Login unless you provided an id value, which you haven't. Commented Mar 20, 2020 at 1:03

2 Answers 2

5

It shows action="/" instead of action="/Home/Login" on the form since you have set the default route as /Home/Login:

app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllerRoute(
            name: "default",
            pattern: "{controller=Home}/{action=Login}/{id?}");
    });

If you change the default route, it will be displayed correctly.

I have tried your code and the problem is in your model binding.

Firstly, you need to remove [FromBody] since you are receiving data from a form instead of request body.

Besides, your form does not post data correctly since you do not set the name attribute or use asp-for tag helper for your inputs which are used to bind inputs' data to model on action parameters.

Finally, see my complete demo with my assumption Login model:

public class Login
{
    public string uname { get; set; }
    public string pwd { get; set; }
}

Login.cshtml( add name attribute which equal the Login model's property names):

<form asp-action="Login" asp-controller="Home" method="post" class="mt-4">
    <div class="row">
        <div class="col-lg-12">
            <div class="form-group">
                <label class="text-dark" for="uname">E-mail</label>
                <input class="form-control" name="uname" id="uname" type="text"
                       placeholder="digite e-mail">
            </div>
        </div>
        <div class="col-lg-12">
            <div class="form-group">
                <label class="text-dark" for="pwd">Senha</label>
                <input class="form-control" name="pwd" id="pwd" type="password"
                       placeholder="digite sua senha">
            </div>
        </div>
        <div class="col-lg-12 text-center">
            <button type="submit" class="btn btn-block btn-dark" />
        </div>

    </div>
</form>

POST action:

[HttpPost]
public async Task<IActionResult> Login(Login LoginModel)
Sign up to request clarification or add additional context in comments.

Comments

1
  1. Make sure you added

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllerRoute(
            name: "default",
            pattern: "{controller=Home}/{action=Login}/{id?}");
    });
    
  2. Make sure you are calling correct action method

  3. In the action method, you are using correct action verb

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.