Click here to Skip to main content
15,887,881 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I am creating a simple login registration page for customers. As I am new to MVC5, I followed some tutorial and created a login page. It was working fine but after creating registration form and making some changes suddenly it is just refreshing the page. I undo all the changes but the result is same.
Here is the code for Controller:
C#
using MyWaayEcommerce.Models;
using MyWaayEcommerce.Models.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using System.Web.UI.WebControls;

namespace MyWaayEcommerce.Controllers
{
    public class AuthController : Controller
    {
        // GET: Auth
        public ActionResult Index()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Index(UserLoginVM lv)
        {
            if (ModelState.IsValid)
            {
                UserManager um = new UserManager();
                if (um.IsEmailExist(lv.UserName))
                {
                    FormsAuthentication.SetAuthCookie(lv.UserName, false);
                    return RedirectToAction("Index", "Customers");
                }
                else
                {
                    return RedirectToAction("Index", "Customers");
                    //ModelState.AddModelError("", "Email or Password does not match");

                }

            }
            return View();
        }
    }
}

UserManagerModel Code:
C#
using MyWaayEcommerce.Models;
using MyWaayEcommerce.Models.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using System.Web.UI.WebControls;

namespace MyWaayEcommerce.Controllers
{
    public class AuthController : Controller
    {
        // GET: Auth
        public ActionResult Index()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Index(UserLoginVM lv)
        {
            if (ModelState.IsValid)
            {
                UserManager um = new UserManager();
                if (um.IsEmailExist(lv.UserName))
                {
                    FormsAuthentication.SetAuthCookie(lv.UserName, false);
                    return RedirectToAction("Index", "Customers");
                }
                else
                {
                    return RedirectToAction("Index", "Customers");
                    //ModelState.AddModelError("", "Email or Password does not match");

                }

            }
            return View();
        }
    }
}

User LoginView Model:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;

namespace MyWaayEcommerce.Models.ViewModels
{
    public class UserLoginVM
    {
        [Required(ErrorMessage = "Email is required")]
        [Display(Name ="Email")]
        public string UserName { get; set; }
        [Required(ErrorMessage = "Password is required")]
        [Display(Name = "Password")]
        public string Password { get; set; }
        [Required(ErrorMessage = "First Name is required")]
        [Display(Name = "First Name")]
        public string FirstName { get; set; }
        [Required(ErrorMessage = "Last Name is required")]
        [Display(Name = "Last Name")]
        public string LastName { get; set; }
        [Required(ErrorMessage = "Street Address is Required")]
        [Display(Name = "Street Address 1")]
        public string Street1 { get; set; }
        [Display(Name = "Street Address 1")]
        public string Street2 { get; set; }
        [Required(ErrorMessage = "City Name is Required")]
        [Display(Name = "City")]
        public string City { get; set; }
        [Required(ErrorMessage = "Province Name is Required")]
        [Display(Name = "Province")]
        public string Province { get; set; }
        [Required(ErrorMessage = "Country Name is Required")]
        [Display(Name = "Country")]
        public string Country { get; set; }
        [Required(ErrorMessage = "Postal Code is Required")]
        [Display(Name = "Postal Code")]
        public string Postal { get; set; }
        [Required(ErrorMessage = "Phone Number is Required")]
        [Display(Name = "Contact Number")]
        public string ContactNumber { get; set; }
    }
}

Login View named Index.cshtml
@model MyWaayEcommerce.Models.ViewModels.UserLoginVM

@{
    ViewBag.Title = "Login";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Login</h2>
@using (Html.BeginForm("Login", "Auth", FormMethod.Post, new { id = "submitForm" }))
{
    @Html.AntiForgeryToken()
    <div class="form-group">
        @Html.EditorFor(model => model.UserName, new { htmlAttributes = new { @class = "form-control" } })
        <br />
        @Html.PasswordFor(model => model.Password, new  { @class = "form-control" } )
        <br />
    </div>
<input type="submit" value="Login" class="btn btn-default" />
}


What I have tried:

I tried to fix that by searching submit button not calling method and came up with form tag parameters but results yet same
Posted
Updated 25-Jan-18 17:00pm

1 solution

I think i see your issue.

In your Html.Begin Form you indicate you are targeting action Login in controller Auth.

C#
@using (Html.BeginForm("Login", "Auth", FormMethod.Post, new { id = "submitForm" }))<


In the above, Login is your Action and Auth is your controller. Action meaning it is the method of a controller.

So in your AuthController you have an Index action but no Login action

C#
public class AuthController : Controller
    {
        [HttpPost]
        public ActionResult Index(UserLoginVM lv)
        {
            if (ModelState.IsValid)
            {
                UserManager um = new UserManager();
                if (um.IsEmailExist(lv.UserName))
                {
                    FormsAuthentication.SetAuthCookie(lv.UserName, false);
                    return RedirectToAction("Index", "Customers");
                }
                else
                {
                    return RedirectToAction("Index", "Customers");
                    //ModelState.AddModelError("", "Email or Password does not match");

                }

            }
            return View();
        }
    }


You need to change the above action from Index to Login and then your code should work (i didn't run it).
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900