title: How to redirect view to homepage - ASP.NET Core MVC
body: <p>This is my first ASP.NET Core MVC project and first C# experience too. I am working on creating a web app that mimics ubereats/doordash.</p>
  <p>Right now, I am coding the register/login system and incorporating that with a SQL database. The register/create account function is working great but now I need to figure out how to authenticate someone when they are logging in.</p>
  <p>So, the way I wrote my algorithm is:</p>
  <ol>
  <li>User creates account - database is updated (working great).</li>
  <li>User clicks login hyperlink - takes them to login page (also working)</li>
  <li>User enters email and password and presses submit (I don't receive any errors here but I don't think I created the form/authenticated correctly).</li>
  <li>Controller takes user entered email and password - if the database finds the email, then check if password is correct.</li>
  <li>if correct - redirect user to the homepage else refresh and present the login form again.</li>
  </ol>
  <p>I need help going through and retrieving items in the database and then verifying if they are correct.</p>
  <p>I also need help redirecting the user to the homepage (which is inside of the &quot;Home&quot; folder in views) if the login is successful. C# and asp.net seems very advanced so I'm wondering if there is an easy function that I don't know about that could be useful.</p>
  <p>Can someone help me getting the login portion working?</p>
  <p>This specific part of the login controller is the issue I think:</p>
  <pre><code>// GET: Logins/Create
  public IActionResult LoginPortal()
  {
      return View();
  }
  
  [HttpPost]
  public async Task&lt;IActionResult&gt; LoginPortal(int id)
  {
          var movie = await _context.Login.FindAsync(id);
  
          if (movie == null)
          {
              return View();
          }
  
          return View(&quot;Home/Index&quot;);
  }
  </code></pre>
  <p>Here is what I have so far:</p>
  <p><strong>LoginsController.cs</strong></p>
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Threading.Tasks;
  using Microsoft.AspNetCore.Mvc;                             //including necessary directories 
  using Microsoft.AspNetCore.Mvc.Rendering;
  using Microsoft.EntityFrameworkCore;
  using EasyEats.Data;
  using EasyEats.Models;
  
  namespace EasyEats.Controllers
  {
      public class LoginsController : Controller                      //log-in &amp; register controller 
      {
          private readonly EasyEatsContext _context;
  
          public LoginsController(EasyEatsContext context)
          {
              _context = context;
          }
  
          // GET: Logins
          public async Task&lt;IActionResult&gt; Index()
          {
              return View(await _context.Login.ToListAsync());
          }
  
          // GET: Logins/Details/5
          public async Task&lt;IActionResult&gt; Details(int? id)
          {
              if (id == null)
              {
                  return NotFound();
              }
  
              var login = await _context.Login
                  .FirstOrDefaultAsync(m =&gt; m.ID == id);
              if (login == null)
              {
                  return NotFound();
              }
  
              return View(login);
          }
  
          // GET: Logins/Create
          public IActionResult Create()
          {
              return View();
          }
  
          // POST: Logins/Create
          // To protect from overposting attacks, enable the specific properties you want to bind to, for 
          // more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
          [HttpPost]
          [ValidateAntiForgeryToken]
          public async Task&lt;IActionResult&gt; Create([Bind(&quot;ID,FirstName,LastName,Address,Email,PhoneNumber,Password&quot;)] Login login)
          {
              if (ModelState.IsValid)
              {
                  _context.Add(login);
                  await _context.SaveChangesAsync();
                  return RedirectToAction(nameof(Index));
              }
              return View(login);
          }
  
  
          // GET: Logins/Create
          public IActionResult LoginPortal()
          {
              return View();
          }
  
          [HttpPost]
          public async Task&lt;IActionResult&gt; LoginPortal(int id)
          {
              var movie = await _context.Login.FindAsync(id);
  
              if (movie == null)
              {
                  return View();
              }
  
              return View(&quot;Home/Index&quot;);
          }
  
          // POST: Logins/Edit/5
          // To protect from overposting attacks, enable the specific properties you want to bind to, for 
          // more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
          [HttpPost]
          [ValidateAntiForgeryToken]
          public async Task&lt;IActionResult&gt; Edit(int id, [Bind(&quot;ID,FirstName,LastName,Address,Email,PhoneNumber,Password&quot;)] Login login)
          {
              if (id != login.ID)
              {
                  return NotFound();
              }
  
              if (ModelState.IsValid)
              {
                  try
                  {
                      _context.Update(login);
                      await _context.SaveChangesAsync();
                  }
                  catch (DbUpdateConcurrencyException)
                  {
                      if (!LoginExists(login.ID))
                      {
                          return NotFound();
                      }
                      else
                      {
                          throw;
                      }
                  }
                  return RedirectToAction(nameof(Index));
              }
              return View(login);
          }
  
          // GET: Logins/Delete/5
          public async Task&lt;IActionResult&gt; Delete(int? id)
          {
              if (id == null)
              {
                  return NotFound();
              }
  
              var login = await _context.Login
                  .FirstOrDefaultAsync(m =&gt; m.ID == id);
              if (login == null)
              {
                  return NotFound();
              }
  
              return View(login);
          }
  
          // POST: Logins/Delete/5
          [HttpPost, ActionName(&quot;Delete&quot;)]
          [ValidateAntiForgeryToken]
          public async Task&lt;IActionResult&gt; DeleteConfirmed(int id)
          {
              var login = await _context.Login.FindAsync(id);
              _context.Login.Remove(login);
              await _context.SaveChangesAsync();
              return RedirectToAction(nameof(Index));
          }
  
          private bool LoginExists(int id)
          {
              return _context.Login.Any(e =&gt; e.ID == id);
          }
      }
  }
  </code></pre>
  <p><strong>Login.cs</strong> (model)</p>
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Threading.Tasks;
  using System.ComponentModel.DataAnnotations;
  using System.ComponentModel.DataAnnotations;
  using System.ComponentModel.DataAnnotations.Schema;
  
  namespace EasyEats.Models
  {
      public class Login                      //class for customer log-in &amp; registration information 
      {
          public int ID { get; set; }
  
          [Required]
          [Display(Name = &quot;First Name&quot;)]
          public string FirstName { get; set; }
  
          [Required]
          [Display(Name = &quot;Last Name&quot;)]
          public string LastName { get; set; }
  
          [Required]
          public string Address { get; set; }
  
          [Required]
          public string Email { get; set; }
  
          [Required]
          [Display(Name = &quot;Phone Number&quot;)]
          public int PhoneNumber { get; set; }
  
          [Required]
          public string Password { get; set; }
      }
  }
  </code></pre>
  <p>Inside my &quot;Views&quot; folder: I have a Home folder, Logins folder and Shared folder</p>
  <p><strong>Index.cshtml</strong> (inside &quot;Home&quot; folder of Views)</p>
  <pre><code>@{
      ViewData[&quot;Title&quot;] = &quot;Home Page&quot;;
  }
  
  &lt;div class=&quot;text-center&quot;&gt;
      &lt;h1 class=&quot;display-4&quot;&gt;Welcome&lt;/h1&gt;
      &lt;p&gt;Learn about &lt;a href=&quot;https://docs.microsoft.com/aspnet/core&quot;&gt;building Web apps with ASP.NET Core&lt;/a&gt;.&lt;/p&gt;
  &lt;/div&gt;
  </code></pre>
  <p><strong>LoginPortal.cshtml</strong> (Inside &quot;Logins&quot; folder in Views)</p>
  <pre><code>@model EasyEats.Models.Login
  
  @{
      ViewData[&quot;Title&quot;] = &quot;Create&quot;;
  }
  
  &lt;hr /&gt;
  &lt;div class=&quot;row&quot;&gt;
      &lt;div class=&quot;col-md-4&quot;&gt;
          &lt;form asp-action=&quot;LoginPortal&quot;&gt;
              &lt;div asp-validation-summary=&quot;ModelOnly&quot; class=&quot;text-danger&quot;&gt;&lt;/div&gt;
              &lt;div class=&quot;form-group&quot;&gt;
                  &lt;label asp-for=&quot;Email&quot; class=&quot;control-label&quot;&gt;&lt;/label&gt;
                  &lt;input asp-for=&quot;Email&quot; class=&quot;form-control&quot; /&gt;
                  &lt;span asp-validation-for=&quot;Email&quot; class=&quot;text-danger&quot;&gt;&lt;/span&gt;
              &lt;/div&gt;
              &lt;div class=&quot;form-group&quot;&gt;
                  &lt;label asp-for=&quot;Password&quot; class=&quot;control-label&quot;&gt;&lt;/label&gt;
                  &lt;input asp-for=&quot;Password&quot; class=&quot;form-control&quot; /&gt;
                  &lt;span asp-validation-for=&quot;Password&quot; class=&quot;text-danger&quot;&gt;&lt;/span&gt;
              &lt;/div&gt;
              &lt;div class=&quot;form-group&quot;&gt;
                  &lt;input type=&quot;submit&quot; value=&quot;Log in&quot; class=&quot;btn btn-primary&quot; /&gt;
              &lt;/div&gt;
          &lt;/form&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  
  @section Scripts {
      @{await Html.RenderPartialAsync(&quot;_ValidationScriptsPartial&quot;);}
  }
  </code></pre>
  <p><strong>_Layout.cshtml</strong> (inside shared folder)</p>
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html lang=&quot;en&quot;&gt;
  &lt;head&gt;
      &lt;meta charset=&quot;utf-8&quot; /&gt;
      &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot; /&gt;
      &lt;title&gt;@ViewData[&quot;Title&quot;] - EasyEats&lt;/title&gt;
      &lt;link rel=&quot;stylesheet&quot; href=&quot;~/lib/bootstrap/dist/css/bootstrap.min.css&quot; /&gt;
      &lt;link rel=&quot;stylesheet&quot; href=&quot;~/css/site.css&quot; /&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;header&gt;
          &lt;nav class=&quot;navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3&quot;&gt;
              &lt;div class=&quot;container&quot;&gt;
                  &lt;a class=&quot;navbar-brand&quot; asp-area=&quot;&quot; asp-controller=&quot;Home&quot; asp-action=&quot;Index&quot;&gt;EasyEats&lt;/a&gt;
                  &lt;button class=&quot;navbar-toggler&quot; type=&quot;button&quot; data-toggle=&quot;collapse&quot; data-target=&quot;.navbar-collapse&quot; aria-controls=&quot;navbarSupportedContent&quot;
                          aria-expanded=&quot;false&quot; aria-label=&quot;Toggle navigation&quot;&gt;
                      &lt;span class=&quot;navbar-toggler-icon&quot;&gt;&lt;/span&gt;
                  &lt;/button&gt;
                  &lt;div class=&quot;navbar-collapse collapse d-sm-inline-flex flex-sm-row-reverse&quot;&gt;
                      &lt;partial name=&quot;_LoginPartial&quot; /&gt;
                      &lt;ul class=&quot;navbar-nav flex-grow-1&quot;&gt;
                          &lt;li class=&quot;nav-item&quot;&gt;
                              &lt;a class=&quot;nav-link text-dark&quot; asp-area=&quot;&quot; asp-controller=&quot;Home&quot; asp-action=&quot;Index&quot;&gt;Home&lt;/a&gt;
                          &lt;/li&gt;
                      &lt;/ul&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
          &lt;/nav&gt;
      &lt;/header&gt;
      &lt;div class=&quot;container&quot;&gt;
          &lt;main role=&quot;main&quot; class=&quot;pb-3&quot;&gt;
              @RenderBody()
          &lt;/main&gt;
      &lt;/div&gt;
  
      &lt;footer class=&quot;border-top footer text-muted&quot;&gt;
          &lt;div class=&quot;container&quot;&gt;
              &amp;copy; 2020 - EasyEats - &lt;a asp-area=&quot;&quot; asp-controller=&quot;Home&quot; asp-action=&quot;Privacy&quot;&gt;Privacy&lt;/a&gt;
          &lt;/div&gt;
      &lt;/footer&gt;
      &lt;script src=&quot;~/lib/jquery/dist/jquery.min.js&quot;&gt;&lt;/script&gt;
      &lt;script src=&quot;~/lib/bootstrap/dist/js/bootstrap.bundle.min.js&quot;&gt;&lt;/script&gt;
      &lt;script src=&quot;~/js/site.js&quot; asp-append-version=&quot;true&quot;&gt;&lt;/script&gt;
      @RenderSection(&quot;Scripts&quot;, required: false)
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  <p><strong>_LoginPartial.cshtml</strong> (inside shared folder)</p>
  <pre><code>&lt;ul class=&quot;navbar-nav&quot;&gt;
      &lt;li class=&quot;nav-item&quot;&gt;
          &lt;a class=&quot;nav-link text-dark&quot; asp-controller=&quot;Logins&quot;
             asp-action=&quot;Create&quot;&gt;Register&lt;/a&gt;
      &lt;/li&gt;
      &lt;li class=&quot;nav-item&quot;&gt;
          &lt;a class=&quot;nav-link text-dark&quot; asp-controller=&quot;Logins&quot;
             asp-action=&quot;LoginPortal&quot;&gt;Login&lt;/a&gt;
      &lt;/li&gt;
  &lt;/ul&gt; 
  </code></pre>
  <p>Here are some pictures:
  <a href="https://i.stack.imgur.com/1n1mB.png" rel="nofollow noreferrer">Homepage</a></p>
  <p><a href="https://i.stack.imgur.com/HZeXJ.png" rel="nofollow noreferrer">Create Account page</a>
  <a href="https://i.stack.imgur.com/vuqZH.png" rel="nofollow noreferrer">Login Page</a></p>