0

So I have a view containing a HTML login form and I need to get the values from the form to a controller via post. Right now I'm trying to display the username of the user through the Content method when the form is submitted, once I know I have the username in the controller I can do the rest. The problem I have is that when I submit the form the username is not displayed, I've outputted to the console to check if the controller action is being called and it isn't.

Any help is greatly appreciated cheers!

View

@{
    @model AdminPanel.Models.cUser
    Layout = null;
    ViewBag.Title = "Login";
}

<head>
    <link href="~/Content/Login.css" rel="stylesheet" type="text/css" />
    <link href="~/Content/Site.css" rel="stylesheet" type="text/css" />
    <link href="~/Content/bootstrap.min.css" rel="stylesheet" type="text/css" />
</head>

<body>
    <!-- Div containing the log in box parent -->
    <div class="log-in-box-parent">
        <h2>LOG IN</h2>

        <div class="log-in-form">
            <form asp-action="Login" asp-controller="Login">
                <input asp-for="Username" type="text" id="Username" placeholder="Username" /> <br />
                <input aso-for="Password" type="password" id="Password" placeholder="Password" /> <br />
                <input type="submit" id="login" value="Log in"/>
            </form>
        </div>
    </div>
</body>

Controller

using AdminPanel.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace AdminPanel.Controllers
{
    public class LoginController : Controller
    {
        // GET: Login
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost, ValidateAntiForgeryToken]
        public ActionResult Login(cUser user)
        {
            return Content($"Hello {user.Username}");
        }
    }
}

And finally my user model

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace AdminPanel.Models
{
    public class cUser
    {
        public string Username { get; set; }
        public string Password { get; set; }
    }
}
15
  • Well, you have a typo in your password element. Commented Jan 10, 2018 at 20:37
  • Thanks, I fixed the typo but it still doesn't work Commented Jan 10, 2018 at 20:38
  • It wasn't intended to fix that issue. Commented Jan 10, 2018 at 20:39
  • 1
    Open your browser dev tools and look at the request. You need to gather diagnostics. Find out why it isn't being called. Commented Jan 10, 2018 at 20:51
  • 1
    asp-for :/ @ADyson Commented Jan 11, 2018 at 7:17

2 Answers 2

1

I noticed that your Login action only accepts POST requests, but your <form tag does not have a method="post" attribute which would tell the form to perform a POST request (instead of a GET). I think you need to set that in order for it to make the correct request to the action method.

<form asp-action="Login" asp-controller="Login" method="post">

https://learn.microsoft.com/en-us/aspnet/core/mvc/views/working-with-forms contains an example of this as well.

Sign up to request clarification or add additional context in comments.

10 Comments

@TheoCrowley "It doesn't work" doesn't tell ADyson anything he can use to make a better answer. You have a wealth of information available to you in your browsers dev tools. Use it.
The problem is exactly the same as before? You should have checked as I have solved it myself using a different method
"You should have checked as I have solved it myself using a different method" That has nothing to do with my point. You need to give better feedback precisely because we cannot do it for you. What response code are you getting? What are your headers? What is the JSON payload? What is the error message in response? Is there an error message? Is it hitting the method? Is it sending the wrong Content-Type? We don't know the answers to any of these questions.
@TheoCrowley it doesn't matter if the issue is "unchanged". We cannot see your screen, nor can we gather diagnostics. A change might produce a different outcome, but still not give the expected result. We don't know. As a result we're shooting in the dark. Only you can provide us with the information we need.
I wont bother making posts like this in the future if im expected to know what all that means to ask a bloody question about HTML forms hahaha.
|
1

Thanks to those of you who tried to help me , I solved the problem by using HTML Helpers instead when creating the form. My edited View can be seen below.

@model AdminPanel.Models.cUser

@{

    Layout = null;
    ViewBag.Title = "Login";
}

<head>
    <link href="~/Content/Login.css" rel="stylesheet" type="text/css" />
    <link href="~/Content/Site.css" rel="stylesheet" type="text/css" />
    <link href="~/Content/bootstrap.min.css" rel="stylesheet" type="text/css" />
</head>

<body>
    <!-- Div containing the log in box parent -->
    <div class="log-in-box-parent">
        <h2>LOG IN</h2>

        <div class="log-in-form">

            @using (Html.BeginForm("Login", "Login", FormMethod.Post))
            {
                @Html.TextBoxFor(model => model.Username, new { placeholder = "Username" })
                @Html.TextBoxFor(model => model.Password, new { placeholder = "Password" })
                <input type="submit" id="login" value="Log in"/>
            }
        </div>
    </div>
</body>

7 Comments

Hmm, interesting that this worked but tag helpers didn't. using (Html.BeginForm("Login", "Login", FormMethod.Post)) should be functionally equivalent to <form asp-action="Login" asp-controller="Login" method="post">. Are you absolutely certain you're using ASP.NET Core, and not traditional ASP.NET? Tag helpers (i.e the asp-for syntax are new in ASP.NET Core and should work they way you showed (with the addition of a method attribute in the case of the form). If you're using the older ASP.NET, then they definitely won't work.
Your other code makes it look more like older ASP.NET than Core as well. e.g. if it was Core you should need using Microsoft.AspNetCore.Mvc; at the start of your controller class, but you haven't got it, you've got the reference to System.Web.Mvc; instead. It looks like you are simply not using the technology you think you are.
If you're new to web dev you've with .NET you've unfortunately arrived at a confusing moment where MS is in the process of transitioning to a new-style framework (.NET Core). There are a lot of similarities to the older .NET but enough differences too. Annoyingly they decided to give these two things sufficiently similar names for it to be potentially confusing, especially to those not familiar with the history. You'll have to pay close attention to exactly which technology you're using, and therefore which documentation etc you need to refer to.
Thanks, I'm pretty sure I'm not using .NET Core. I'm out at the moment and can't check. Should I be using .NET Core instead or should I just stick with .NET? Cheers
These links might help you understand a bit better and make a good decision ("latest" is not always "best", for one thing, and also both frameworks are still fully supported, you're not working on a legacy platform if you choose .NET). There are good reasons for choosing either: learn.microsoft.com/en-us/dotnet/standard/… and c-sharpcorner.com/article/…
|

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.