title: Validate login form ASP.NET MVC
body: <p>so I am trying to make my login form to work. I have one table on my database that I want to be able to log in with. The table has two rows, username and password, and when user types in correctly, it should be redirected to the correct page. But when I press the button, nothing happens, what am I doing wrong here?</p>
  
  <p>Model:</p>
  
  <pre><code>namespace Barndomshem.Models
  {
      public class User
      {
          public string Username { get; set; }
          public string Password { get; set; }
      }
  }
  </code></pre>
  
  <p>View:</p>
  
  <pre><code>&lt;div class="container"&gt;
      &lt;div class="row"&gt;
          &lt;div class="box"&gt;
              &lt;div class="col-lg-12"&gt;
                  &lt;form class="form-wrapper" id="contact-form" method="post" role="form" novalidate&gt;
                      &lt;div class="form-group"&gt;
                          &lt;div class="row"&gt;
                              &lt;div class="form-group col-lg-4"&gt;
                                  &lt;label for="name"&gt;
                                      Användarnamn
                                  &lt;/label&gt;
                                  &lt;input type="text" id="name" name="name" class="form-control" data-errmsg="Fyll i användarnamn."
                                         placeholder="Ditt Användarnamn" required /&gt;
                              &lt;/div&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
                      &lt;div class="form-group"&gt;
                          &lt;div class="row"&gt;
                              &lt;div class="form-group col-lg-4"&gt;
                                  &lt;label for="number"&gt;
                                      Lösenord
                                  &lt;/label&gt;
                                  &lt;input type="text" id="number" name="number" class="form-control" data-errmsg="Fyll i lösenord."
                                         placeholder="Ditt Lösenord" /&gt;
                              &lt;/div&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
                      &lt;div class="row"&gt;
                          &lt;div class="col-md-2 col-sm-2 offset2"&gt;
                              &lt;input type="submit" value="Skicka" class="btn btn-primary" /&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
                  &lt;/form&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>Controller:</p>
  
  <pre><code>using System.Web.Mvc;
  using System.Data;
  using System.Data.SqlClient;
  using Barndomshem.Models;
  
  
  namespace Barndomshem.Controllers
  {
      public class RapportController : Controller
      {
          SqlConnection connection = new SqlConnection(@"Data Source=.\SQLExpress;Initial Catalog=Barndomshem;Integrated Security=True");
          SqlCommand command = new SqlCommand();
          SqlDataReader reader;
  
          public ActionResult Index()
          {
              var user = new User();
  
              Session["UserName"] = user;
  
              if (Session["UserName"] == null)
              {
                  return RedirectToAction("/Rapport/Validate");
              }
  
              return View();
          }
  
          public ActionResult Validate(User user)
          {
              var query = command.CommandText = "SELECT Username FROM User";
              command.CommandType = CommandType.Text;
              command.Connection = connection;
  
              connection.Open();
  
              if (user.Username == query)
              {
                  return RedirectToAction("/Rapport", user);
              }
  
              connection.Close();
  
              return View();
          }
      }
  }
  </code></pre>